Skip to main content

Write a C Program that uses functions to perform the following operations on a single linked list Creation, Insertion, Deletion, Traversal

 

#include <stdio.h>

#include <stdlib.h>

 

struct node {

  int data;

  struct node *next;

};

 

struct node *start = NULL;

void insert_at_begin(int);

void insert_at_end(int);

void traverse();

void delete_from_begin();

void delete_from_end();

int count = 0;

 

int main () {

  int i, data;

 

  for (;;) {

    printf("1. Insert an element at the beginning of linked list.\n");

    printf("2. Insert an element at the end of linked list.\n");

    printf("3. Traverse linked list.\n");

    printf("4. Delete an element from beginning.\n");

    printf("5. Delete an element from end.\n");

    printf("6. Exit\n");

 

    scanf("%d", &i);

 

    if (i == 1) {

      printf("Enter value of element\n");

      scanf("%d", &data);

      insert_at_begin(data);

    }

    else if (i == 2) {

      printf("Enter value of element\n");

      scanf("%d", &data);

      insert_at_end(data);

    }

    else if (i == 3)

      traverse();

    else if (i == 4)

      delete_from_begin();

    else if (i == 5)

      delete_from_end();

 

    else if (i == 6)

      break;

 

    else

      printf("Please enter valid input.\n");

  }

 

       

  return 0;

}

 

void insert_at_begin(int x) {

  struct node *t;

 

  t = (struct node*)malloc(sizeof(struct node));

  t->data = x;

  count++;

 

  if (start == NULL) {

    start = t;

    start->next = NULL;

    return;

  }

 

  t->next = start;

  start = t;

}

 

void insert_at_end(int x) {

  struct node *t, *temp;

 

  t = (struct node*)malloc(sizeof(struct node));

  t->data = x;

  count++;

 

 

 

  if (start == NULL) {

    start = t;

    start->next = NULL;

    return;

  }

 

  temp = start;

 

  while (temp->next != NULL)

    temp = temp->next;

 

  temp->next = t;

  t->next   = NULL;

}

 

void traverse() {

  struct node *t;

 

  t = start;

 

  if (t == NULL) {

    printf("Linked list is empty.\n");

    return;

  }

 

  printf("There are %d elements in linked list.\n", count);

 

  while (t->next != NULL) {

    printf("%d\n", t->data);

    t = t->next;

  }

  printf("%d\n", t->data); // Print last node

}

 

 

 

void delete_from_begin() {

  struct node *t;

  int n;

 

  if (start == NULL) {

    printf("Linked list is empty.\n");

    return;

  }

 

  n = start->data;

  t = start->next;

  free(start);

  start = t;

  count--;

 

  printf("%d deleted from the beginning successfully.\n", n);

}

 

void delete_from_end() {

  struct node *t, *u;

  int n;

 

  if (start == NULL) {

    printf("Linked list is empty.\n");

    return;

  }

 

  count--;

 

  if (start->next == NULL) {

    n = start->data;

    free(start);

    start = NULL;

    printf("%d deleted from end successfully.\n", n);

    return;

  }

 

  t = start;

 

  while (t->next != NULL) {

    u = t;

    t = t->next;

  }

 

  n = t->data;

  u->next = NULL;

  free(t);

 

  printf("%d deleted from end successfully.\n", n);

 

}

 


Comments

Popular posts from this blog

Windows 11 Pro Activation Key

 Windows 11 Pro key: W269N-WFGWX-YVC9B-4J6C9-T83GX Windows 11 Pro N key: MH37W-N47XK-V7XM9-C7227-GCQG9 Windows 11 Pro Workstations key: NRG8B-VKK3Q-CXVCJ-9G2XF-6Q84J Windows 11 Pro Workstations N key: 9FNHH-K3HBT-3W4TD-6383H-6XYWF Windows 11 Pro Education key: 6TP4R-GNPTD-KYYHQ-7B7DP-J447Y Windows 11 Home key: TX9XD-98N7V-6WMQ6-BX7FG-H8Q99 Windows 11 Home N key: 3KHY7-WNT83-DGQKR-F7HPR-844BM Windows 11 Home Home Single Language key: 7HNRX-D7KGG-3K4RQ-4WPJ4-YTDFH Windows 11 Home Country Specific: PVMJN-6DFY6-9CCP6-7BKTT-D3WVR Windows 11 Education key: NW6C2-QMPVW-D7KKK-3GKT6-VCFB2 Windows 11 Education N: 2WH4N-8QGBV-H22JP-CT43Q-MDWWJ Windows 11 Enterprise key: NPPR9-FWDCX-D2C8J-H872K-2YT43 Windows 11 Enterprise N key: DPH2V-TTNVB-4X9Q3-TJR4H-KHJW4 Windows 11 Enterprise G: YYVX9-NTFWV-6MDM3-9PT4T-4M68B Windows 11 Enterprise G N: 44RPN-FTY23-9VTTB-MP9BX-T84FV Windows 11 Enterprise LTSC 2019 key: M7XTQ-FN8P6-TTKYV-9D4CC-J462D Windows 11 Enterprise N LTSC 2019 key: 92NFX-8DJQP-P6BBQ-THF...

Create Private folder in windows

 @ECHO OFF if EXIST "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" goto UNLOCK if NOT EXIST Private goto MDPrivate :CONFIRM echo Are you sure to lock this folder? (Y/N) set/p "cho=>" if %cho%==Y goto LOCK if %cho%==y goto LOCK if %cho%==n goto END if %cho%==N goto END echo Invalid choice. goto CONFIRM :LOCK ren Private "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" attrib +h +s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" echo Folder locked goto End :UNLOCK echo Enter password to Unlock Your Secure Folder set/p "pass=>" if NOT %pass%== 121 goto FAIL attrib -h -s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" ren "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" Private echo Folder Unlocked successfully goto End :FAIL echo Invalid password goto end :MDPrivate md Private echo Private created successfully goto End :End RAW Paste Data @ECHO OFF if EXIST "Control Panel.{21...