Advertisement
tsnaik

Linked list operations

Oct 22nd, 2014
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.63 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct node
  5. {
  6.     int data;
  7.     struct node *next;
  8. } *start=NULL;
  9.  
  10. void add_start();
  11. void add_end();
  12. void delete_start();
  13. void delete_end();
  14. void print();
  15.  
  16. int main()
  17. {
  18.     int choice;
  19.     //system("cls");
  20.     system("cls");
  21.     while(1)
  22.     {
  23.  
  24.         printf("\nEnter choice");
  25.             printf("\n1) Print linked list\n2)Add element at start\n3)Add element at end\n4)Clear linked list\n5)Delete element at start\n6)Delete element at end\n7)Exit the program\n");
  26.             scanf("%d",&choice);
  27.  
  28.         switch(choice)
  29.         {
  30.             case 1: system("cls");
  31.                 print();
  32.                 break;
  33.  
  34.             case 2: system("cls");
  35.                 add_start();
  36.                 break;
  37.             case 3: system("cls");
  38.                 add_end();
  39.                 break;
  40.             case 4: start=NULL;
  41.                 system("cls");
  42.                 printf("\nCleared linked list. (Memory didn't get freed. Wait for update :P)\n");
  43.                 break;
  44.             case 5: system("cls");
  45.                 delete_start();
  46.                 break;
  47.             case 6: system("cls");
  48.                 delete_end();
  49.                 break;
  50.             case 7: exit(0);
  51.                 break;
  52.             default: printf("\nInvalid");
  53.         }
  54.  
  55.     }
  56. return 0;
  57. }
  58.  
  59. void add_start()
  60. {
  61.  
  62.     struct node *new;
  63.     new=(struct node *) (malloc(sizeof(struct node)));
  64.  
  65.     printf("\nEnter data to add at start:");
  66.         scanf("%d",&(new->data));
  67.  
  68.     new->next=NULL;
  69.     if(start==NULL)
  70.     {
  71.         start=new;
  72.  
  73.         return;
  74.     }
  75.     new->next=start;
  76.  
  77.     start=new;
  78. }
  79.  
  80. void print()
  81. {
  82.     if(start==NULL)
  83.     {
  84.         printf("\nNo data in Linked List\n");
  85.         return;
  86.     }
  87.  
  88.         struct node *p;
  89.         p=start;
  90.         printf("\nLinked list: \n");
  91.         while(p!=NULL)
  92.         {
  93.             printf("\n%d",p->data);
  94.             p=p->next;
  95.         }
  96.  
  97.  
  98.  
  99. }
  100.  
  101. void add_end()
  102. {
  103.  
  104.         struct node *new;
  105.         new=(struct node *) (malloc(sizeof(struct node)));
  106.  
  107.     new->next=NULL;
  108.         printf("\nEnter data to add at end:");
  109.         scanf("%d",&(new->data));
  110.  
  111.     struct node *p;
  112.     p=start;
  113.  
  114.     if(start==NULL)
  115.     {
  116.         start=new;
  117.         return;
  118.     }
  119.  
  120.     while(p->next!=NULL)
  121.     {
  122.         p=p->next;
  123.     }
  124.  
  125.     p->next=new;
  126. }
  127.  
  128. void delete_start()
  129.  {
  130.     struct node *p;
  131.  
  132.     if(start==NULL)
  133.      {
  134.          printf("\nLink list is empty\n");
  135.         return;
  136.      }
  137.      else
  138.      {
  139.         p=start;
  140.         start=start->next;
  141.         free(p);
  142.         return;
  143.      }
  144.  }
  145.  
  146. void delete_end()
  147.  {
  148.      struct node *p,*n;
  149.  
  150.      if(start==NULL)
  151.      {
  152.          printf("\nLink List Is Empty\n");
  153.          return;
  154.      }
  155.      else
  156.      {
  157.         p=start;
  158.  
  159.        if(p->next!=NULL)
  160.        {
  161.          while(p->next->next!=NULL)
  162.          p=p->next;
  163.  
  164.          n=p->next;
  165.          p->next=NULL;
  166.  
  167.          free(n);
  168.  
  169.          return;
  170.        }
  171.        else
  172.        {
  173.           start=NULL;
  174.           free(p);
  175.           return;
  176.        }
  177.  
  178.      }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement