Advertisement
Unique144

Linked List

Oct 27th, 2021
1,140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.60 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct node {
  5.     int data;
  6.     struct node *next;
  7. };
  8.  
  9. struct node *start = NULL;
  10.  
  11. void insertAtStart() {
  12.     struct node *temp = (struct node*)malloc(sizeof(struct node));
  13.     printf("Enter some data\n");
  14.     scanf("%d", &temp->data);
  15.     temp->next = start;
  16.     start = temp;
  17. }
  18.  
  19. void insertAtLast() {
  20.     //create a node
  21.     struct node *temp = (struct node*)malloc(sizeof(struct node));
  22.     temp -> next = NULL;
  23.     printf("Enter some data\n");
  24.     scanf("%d", &temp->data);
  25.     if(start == NULL)
  26.     start = temp;
  27.     else {
  28.         struct node*x= start;
  29.         while(x->next!=NULL)
  30.         x=x->next;
  31.         x->next = temp;
  32.     }
  33. }
  34.  
  35. void insertAfter(int k) {
  36.     struct node *temp = (struct node*)malloc(sizeof(struct node));
  37.     printf("Enter some data\n");
  38.     scanf("%d", &temp->data);
  39.     struct node *x=start;
  40.     while(x!=NULL && x->data!=k ) {
  41.         x=x->next;
  42.     }
  43.     if(x != NULL) {
  44.         temp->next = x->next;
  45.         x->next = temp;
  46.     }
  47.     else
  48.     printf("Data not found");
  49.    
  50. }
  51.  
  52. void insertBefore(int k) {
  53.     struct node *temp = (struct node*)malloc(sizeof(struct node));
  54.     printf("Enter some data\n");
  55.     scanf("%d", &temp->data);
  56.     struct node *x=start;
  57.     struct node *y=NULL;
  58.     while(x!=NULL && x->data!=k ) {
  59.         y=x;
  60.         x=x->next;
  61.     }
  62.     if(x != NULL) {
  63.         if(y==NULL) {
  64.             temp->next = start;
  65.             start = temp;
  66.         }  
  67.         else {
  68.             y->next = temp;
  69.             temp->next = x;
  70.         }
  71.     }
  72.     else
  73.     printf("Data not found");
  74. }
  75.  
  76. void display() {
  77.     if(start == NULL) printf("Empty Linked List");
  78.     else {
  79.         struct node*x=start;
  80.         while(x!=NULL) {
  81.             printf("%d ", x->data);
  82.             x=x->next;
  83.         }
  84.     }
  85.    
  86. }
  87.  
  88. void DeleteFirst() {
  89.     if(start!=NULL)
  90.     start = start-> next;
  91.     else
  92.     printf("Already Empty");
  93. }
  94.  
  95. void DeleteLast() {
  96.     if(start!=NULL) {
  97.         struct node *q=NULL;
  98.         struct node *p=start;
  99.         while(p->next!=NULL) {
  100.             q = p;
  101.             p = p->next;   
  102.         }
  103.         if(q!=NULL)
  104.         {
  105.             q->next=NULL;
  106.         }
  107.         else
  108.         {
  109.             start = NULL;
  110.         }
  111.         }
  112. }
  113.  
  114. void deleteByValue(int m) {
  115.     if(start==NULL) {
  116.         printf("Empty Array");
  117.     }
  118.     else {
  119.         struct node *p = start;
  120.         struct node *q = NULL;
  121.         while(p->data!=m) {
  122.             q=p;
  123.             p=p->next;
  124.         }
  125.         if(p==NULL)
  126.             printf("Element not Found.\n");
  127.         else if(q==NULL)
  128.             start = p->next;
  129.         else
  130.             q->next = p->next;
  131.     }
  132. }
  133.  
  134. void reverse() {
  135.     struct node *p=start;
  136.     struct node *r= NULL;
  137.     struct node *q= NULL;
  138.     do {
  139.         r=q;
  140.         q=p;
  141.         if(p!=NULL) {
  142.             p=p->next;
  143.             q->next=r;
  144.         }
  145.         else {
  146.             printf("Empty List!");
  147.         }
  148.     }
  149.     while (p!=NULL);
  150.    
  151.     start = q;
  152. }
  153.  
  154.  
  155. main() {
  156.     int c=0;
  157.     do {
  158.         printf("\n\nMenu:\n--------------------\n");
  159.         printf("Display: 1\nInsert at Last: 2\nInsert at Start: 3\nInsert element at a certain position: 4\nInsert element before a certain element: 5\n");
  160.         printf("Delete First: 6\n");
  161.         printf("Delete Last: 7\n");
  162.         printf("Delete By Value: 8\n");
  163.         printf("Reverse: 9\n");
  164.         printf("Exit: 10\n");
  165.         scanf("%d", &c);
  166.         switch(c) {
  167.             case 1:
  168.             display();
  169.             break;
  170.             case 2:
  171.             insertAtLast();
  172.             break;
  173.             case 3:
  174.             insertAtStart();
  175.             break;
  176.             case 4:
  177.             printf("Enter the element after which data has to be inserted.\n");
  178.             int k;
  179.             scanf("%d", &k);
  180.             insertAfter(k);
  181.             break;
  182.             case 5:
  183.             printf("Enter the element before which data has to be inserted.\n");
  184.             int l;
  185.             scanf("%d", &l);
  186.             insertBefore(l);
  187.             break;
  188.             case 6:
  189.                 DeleteFirst();
  190.                 break;
  191.             case 7:
  192.                 DeleteLast();
  193.                 break;
  194.             case 8:
  195.                 printf("Enter the value:\n");
  196.                 int m;
  197.                 scanf("%d", &m);
  198.                 deleteByValue(m);
  199.                 break;
  200.             case 9:
  201.                 reverse();
  202.                 break;
  203.             case 10:
  204.             printf("Breaking out of the loop");
  205.         }
  206.     }
  207.     while(c!=10);
  208. }
  209.  
  210.  
  211.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement