Virajsinh

Linked_List

Dec 6th, 2017
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.23 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4.  
  5. struct node
  6. {
  7.     int data;
  8.     struct node *next;
  9. };
  10.  
  11. struct node *start = NULL;
  12. struct node *create_ll(struct node *);
  13. struct node *display(struct node *);
  14. struct node *insert_beg(struct node *);
  15. struct node *insert_end(struct node *);
  16. struct node *insert_before(struct node *);
  17. struct node *insert_after(struct node *);
  18. struct node *delete_beg(struct node *);
  19. struct node *delete_end(struct node *);
  20. struct node *delete_node(struct node *);
  21. struct node *delete_after(struct node *);
  22. struct node *delete_list(struct node *);
  23. struct node *sort_list(struct node *);
  24.  
  25. void main()
  26. {
  27.     int option;
  28.     do
  29.     {
  30.         printf("\n\n *****MAIN MENU *****");
  31.         printf("\n 1: Create a list");
  32.         printf("\n 2: Display the list");
  33.         printf("\n 3: Add a node at the beginning");
  34.         printf("\n 4: Add a node at the end");
  35.         printf("\n 5: Add a node before a given node");
  36.         printf("\n 6: Add a node after a given node");
  37.         printf("\n 7: Delete a node from the beginning");
  38.         printf("\n 8: Delete a node from the end");
  39.         printf("\n 9: Delete a given node");
  40.         printf("\n 10: Delete a node after a given node");
  41.         printf("\n 11: Delete the entire list");
  42.         printf("\n 12: Sort the list");
  43.         printf("\n 13: EXIT");
  44.         printf("\n\n Enter your option : ");
  45.         scanf("%d", &option);
  46.         switch(option)
  47.         {
  48.             case 1:
  49.                 start = create_ll(start);
  50.                 printf("\n LINKED LIST CREATED");
  51.                 break;
  52.             case 2:
  53.                 start = display(start);
  54.                 break;
  55.             case 3:
  56.                 start = insert_beg(start);
  57.                 break;
  58.             case 4:
  59.                 start = insert_end(start);
  60.                 break;
  61.             case 5:
  62.                 start = insert_before(start);
  63.                 break;
  64.             case 6:
  65.                 start = insert_after(start);
  66.                 break;
  67.             case 7:
  68.                 start = delete_beg(start);
  69.                 break;
  70.             case 8:
  71.                 start = delete_end(start);
  72.                 break;
  73.             case 9:
  74.                 start = delete_node(start);
  75.                 break;
  76.             case 10:
  77.                 start = delete_after(start);
  78.                 break;
  79.             case 11:
  80.                 start = delete_list(start);
  81.                 printf("\n LINKED LIST DELETED");
  82.                 break;
  83.             case 12:
  84.                 start = sort_list(start);
  85.                 break;
  86.         }
  87.     }while(option !=13);
  88.     getch();
  89. }
  90.  
  91. struct node *create_ll(struct node *start)
  92. {
  93.     struct node *new_node, *ptr;
  94.     int num;
  95.     printf("\n Enter -1 to end");
  96.     printf("\n Enter the data : ");
  97.     scanf("%d", &num);
  98.     while(num!=-1)
  99.     {
  100.         new_node = (struct node*)malloc(sizeof(struct node));
  101.         new_node -> data=num;
  102.         if(start==NULL)
  103.         {
  104.             new_node -> next = NULL;
  105.             start = new_node;
  106.         }
  107.         else
  108.         {
  109.             ptr=start;
  110.             while(ptr->next!=NULL)
  111.             ptr=ptr->next;
  112.             ptr->next = new_node;
  113.             new_node->next=NULL;
  114.         }
  115.         printf("\n Enter the data : ");
  116.         scanf("%d", &num);
  117.     }
  118.     return start;
  119. }
  120. struct node *display(struct node *start)
  121. {
  122.     struct node *ptr;
  123.     ptr = start;
  124.     while(ptr != NULL)
  125.     {
  126.         printf("\t %d", ptr -> data);
  127.         ptr = ptr -> next;
  128.     }
  129.     return start;
  130. }
  131. struct node *insert_beg(struct node *start)
  132. {
  133.     struct node *new_node;
  134.     int num;
  135.     printf("\n Enter the data : ");
  136.     scanf("%d", &num);
  137.     new_node = (struct node *)malloc(sizeof(struct node));
  138.     new_node -> data = num;
  139.     new_node -> next = start;
  140.     start = new_node;
  141.     return start;
  142. }
  143. struct node *insert_end(struct node *start)
  144. {
  145.     struct node *ptr, *new_node;
  146.     int num;
  147.     printf("\n Enter the data : ");
  148.     scanf("%d", &num);
  149.     new_node = (struct node *)malloc(sizeof(struct node));
  150.     new_node -> data = num;
  151.     new_node -> next = NULL;
  152.     ptr = start;
  153.     while(ptr -> next != NULL)
  154.         ptr = ptr -> next;
  155.     ptr -> next = new_node;
  156.     return start;
  157. }
  158. struct node *insert_before(struct node *start)
  159. {
  160.     struct node *new_node, *ptr, *preptr;
  161.     int num, val;
  162.     printf("\n Enter the data : ");
  163.     scanf("%d", &num);
  164.     printf("\n Enter the value before which the data has to be inserted : ");
  165.     scanf("%d", &val);
  166.     new_node = (struct node *)malloc(sizeof(struct node));
  167.     new_node -> data = num;
  168.     ptr = start;
  169.     while(ptr -> data != val)
  170.     {
  171.         preptr = ptr;
  172.         ptr = ptr -> next;
  173.     }
  174.     preptr -> next = new_node;
  175.     new_node -> next = ptr;
  176.     return start;
  177. }
  178. struct node *insert_after(struct node *start)
  179. {
  180.     struct node *new_node, *ptr, *preptr;
  181.     int num, val;
  182.     printf("\n Enter the data : ");
  183.     scanf("%d", &num);
  184.     printf("\n Enter the value after which the data has to be inserted : ");
  185.     scanf("%d", &val);
  186.     new_node = (struct node *)malloc(sizeof(struct node));
  187.     new_node -> data = num;
  188.     ptr = start;
  189.     preptr = ptr;
  190.     while(preptr -> data != val)
  191.     {
  192.         preptr = ptr;
  193.         ptr = ptr -> next;
  194.     }
  195.     preptr -> next=new_node;
  196.     new_node -> next = ptr;
  197.     return start;
  198. }
  199. struct node *delete_beg(struct node *start)
  200. {
  201.     struct node *ptr;
  202.     ptr = start;
  203.     start = start -> next;
  204.     free(ptr);
  205.     return start;
  206. }
  207. struct node *delete_end(struct node *start)
  208. {
  209.     struct node *ptr, *preptr;
  210.     ptr = start;
  211.     while(ptr -> next != NULL)
  212.     {
  213.         preptr = ptr;
  214.         ptr = ptr -> next;
  215.     }
  216.     preptr -> next = NULL;
  217.     free(ptr);
  218.     return start;
  219. }
  220. struct node *delete_node(struct node *start)
  221. {
  222.     struct node *ptr, *preptr;
  223.     int val;
  224.     printf("\n Enter the value of the node which has to be deleted : ");
  225.     scanf("%d", &val);
  226.     ptr = start;
  227.     if(ptr -> data == val)
  228.     {
  229.         start = delete_beg(start);
  230.         return start;
  231.     }
  232.     else
  233.     {
  234.         while(ptr -> data != val)
  235.         {
  236.             preptr = ptr;
  237.             ptr = ptr -> next;
  238.         }
  239.         preptr -> next = ptr -> next;
  240.         free(ptr);
  241.     }
  242.     return start;
  243.  
  244. }
  245. struct node *delete_after(struct node *start)
  246. {
  247.     struct node *ptr, *preptr;
  248.     int val;
  249.     printf("\n Enter the value after which the node has to deleted : ");
  250.     scanf("%d", &val);
  251.     ptr = start;
  252.     preptr = ptr;
  253.     while(preptr -> data != val)
  254.     {
  255.         preptr = ptr;
  256.         ptr = ptr -> next;
  257.     }
  258.     preptr -> next=ptr -> next;
  259.     free(ptr);
  260.     return start;
  261. }
  262. struct node *delete_list(struct node *start)
  263. {
  264.     struct node *ptr;
  265.     ptr=start;
  266.     while(ptr!=NULL)
  267.     {
  268.         start=delete_beg(ptr);
  269.         ptr=start;
  270.     }
  271.     free(start);
  272.     return start;
  273. }
  274. struct node *sort_list(struct node *start)
  275. {
  276.     struct node *ptr1, *ptr2;
  277.     int temp;
  278.     ptr1 = start;
  279.     while(ptr1 -> next != NULL)
  280.     {
  281.         ptr2 = ptr1 -> next;
  282.         while(ptr2 != NULL)
  283.         {
  284.             if(ptr1 -> data > ptr2 -> data)
  285.             {
  286.                 temp = ptr1 -> data;
  287.                 ptr1 -> data = ptr2 -> data;
  288.                 ptr2 -> data = temp;
  289.             }
  290.             ptr2 = ptr2 -> next;
  291.         }
  292.         ptr1 = ptr1 -> next;
  293.     }
  294.     return start; // Had to be added
  295. }
Add Comment
Please, Sign In to add comment