splash365

singly_linked_list

Jan 8th, 2021 (edited)
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.24 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. };
  9.  
  10. struct Node *head = NULL;
  11.  
  12. void CreatAList(int n)
  13. {
  14.     for(int i=0; i<n; i++)
  15.     {
  16.         int val;
  17.         scanf("%d",&val);
  18.         struct Node *newNode = (struct Node*) malloc(sizeof(struct Node));
  19.         newNode -> data = val;
  20.         newNode -> next = NULL;
  21.         if(head == NULL)
  22.         {
  23.             head = newNode;
  24.         }
  25.         else
  26.         {
  27.             struct Node *cur;
  28.             cur = head;
  29.             while(cur -> next != NULL)
  30.             {
  31.                 cur = cur -> next;
  32.             }
  33.             cur -> next = newNode;
  34.         }
  35.     }
  36. }
  37.  
  38. void insertFirst(int val)
  39. {
  40.     struct Node *newNode = (struct Node*) malloc(sizeof(struct Node));
  41.     newNode -> data = val;
  42.     newNode -> next = NULL;
  43.     if(head == NULL)
  44.     {
  45.         head = newNode;
  46.     }
  47.     else
  48.     {
  49.         newNode -> next = head;
  50.         head = newNode;
  51.     }
  52. }
  53.  
  54. void insertLast(int val)
  55. {
  56.     struct Node *newNode = (struct Node*) malloc(sizeof(struct Node));
  57.     newNode -> data = val;
  58.     newNode -> next = NULL;
  59.  
  60.     if(head == NULL)
  61.     {
  62.         head = newNode;
  63.     }
  64.     else
  65.     {
  66.         struct Node *cur = head;
  67.         while(cur -> next != NULL)
  68.         {
  69.             cur = cur -> next;
  70.         }
  71.         cur -> next = newNode;
  72.     }
  73. }
  74.  
  75. void insertAt(int pos,int val)
  76. {
  77.     if(pos==1)
  78.     {
  79.         insertFirst(val);
  80.         return;
  81.     }
  82.     if(head==NULL)
  83.     {
  84.         printf("No linked list is being created\n");
  85.         return;
  86.     }
  87.     struct Node *newNode = (struct Node*) malloc(sizeof(struct Node));
  88.     newNode -> data = val;
  89.     newNode -> next = NULL;
  90.  
  91.     struct Node *temp;
  92.     temp = head;
  93.  
  94.     for(int i=2; i<pos; i++)
  95.     {
  96.         temp = temp -> next;
  97.         if(temp==NULL) {
  98.             printf("Insertion is not possible\n");
  99.             return;
  100.         }
  101.     }
  102.     newNode -> next = temp -> next;
  103.     temp -> next = newNode;
  104. }
  105.  
  106. void deletefirst()
  107. {
  108.     if(head == NULL)
  109.     {
  110.         printf("List is empty\n");
  111.         return;
  112.     }
  113.     else
  114.     {
  115.         struct Node *cur;
  116.         cur = head;
  117.         head = head -> next;
  118.         free(cur);
  119.         printf("Successfully Deleted!\n\n");
  120.     }
  121. }
  122.  
  123. void deleteLast()
  124. {
  125.     if(head == NULL)
  126.     {
  127.         printf("List is empty\n");
  128.         return;
  129.     }
  130.     else
  131.     {
  132.         struct Node *cur, *prev;
  133.         cur = head;
  134.         prev = head;
  135.         while(cur -> next != NULL)
  136.         {
  137.             prev = cur;
  138.             cur = cur -> next;
  139.         }
  140.         if(cur == head) head = NULL;
  141.         else prev -> next = NULL;
  142.         free(cur);
  143.         printf("Successfully Deleted!\n\n");
  144.     }
  145. }
  146.  
  147. void deleteAt(int pos)
  148. {
  149.     if(head == NULL)
  150.     {
  151.         printf("List is empty\n");
  152.         return;
  153.     }
  154.     else
  155.     {
  156.         struct Node *cur, *prev;
  157.         cur = head;
  158.         prev = head;
  159.         for(int i=1; i<pos; i++)
  160.         {
  161.             if(cur == NULL)
  162.             {
  163.                 printf("Deletion is not possible!\n");
  164.                 return;
  165.             }
  166.             prev = cur;
  167.             cur = cur -> next;
  168.         }
  169.         if(cur == head) head = NULL;
  170.         else
  171.         {
  172.             prev -> next = cur -> next;
  173.         }
  174.         free(cur);
  175.         printf("Successfully Deleted!\n\n");
  176.     }
  177. }
  178.  
  179. void search(int val)
  180. {
  181.     if(head==NULL)
  182.     {
  183.         printf("List is empty!\n");
  184.         return;
  185.     }
  186.     int index = 1;
  187.     struct Node *cur;
  188.     cur = head;
  189.     while(cur != NULL)
  190.     {
  191.         if(cur -> data == val)
  192.         {
  193.             printf("%d found at position: %d\n",val,index);
  194.             return;
  195.         }
  196.         cur = cur -> next;
  197.         index++;
  198.     }
  199.     printf("%d is not found in the list\n",val);
  200. }
  201.  
  202. void printNode(struct Node* cur)
  203. {
  204.     printf("Linked List: ");
  205.     while(cur != NULL)
  206.     {
  207.         printf("%d  ",cur->data);
  208.         cur = cur -> next;
  209.     }
  210.     printf("\n\n");
  211. }
  212.  
  213. void freeTheList()
  214. {
  215.     struct Node *curNode = head;
  216.     while(head != NULL)
  217.     {
  218.         curNode = head;
  219.         head = head->next;
  220.         free(curNode);
  221.     }
  222. }
  223.  
  224. int main()
  225. {
  226.     int n;
  227.     printf("Enter the number of elements in the list: ");
  228.     scanf("%d",&n);
  229.     printf("Enter %d elements: ", n);
  230.     CreatAList(n);
  231.     printNode(head);
  232.     printf("A linked list has been created!\n\n\n");
  233.  
  234.     while(1)
  235.     {
  236.         printf("1. Insert First\n");
  237.         printf("2. Insert Last\n");
  238.         printf("3. Insert At\n");
  239.         printf("4. Delete First\n");
  240.         printf("5. Delete Last\n");
  241.         printf("6. Delete At\n");
  242.         printf("7. Search\n");
  243.         printf("8. Print List\n");
  244.         printf("9. Exit\n");
  245.         printf("Enter Choice: ");
  246.         int choice;
  247.         scanf("%d",&choice);
  248.         if(choice == 1)
  249.         {
  250.             printf("Enter the element: ");
  251.             int val;
  252.             scanf("%d",&val);
  253.             insertFirst(val);
  254.             printf("Successfully inserted!\n\n");
  255.         }
  256.         else if(choice == 2)
  257.         {
  258.             printf("Enter the element: ");
  259.             int val;
  260.             scanf("%d",&val);
  261.             insertLast(val);
  262.             printf("Successfully inserted!\n\n");
  263.         }
  264.         else if(choice == 3)
  265.         {
  266.             printf("Enter the position (between first and last): ");
  267.             int pos;
  268.             scanf("%d",&pos);
  269.             printf("Enter the element: ");
  270.             int val;
  271.             scanf("%d",&val);
  272.             insertAt(pos,val);
  273.             printf("Successfully inserted!\n\n");
  274.         }
  275.         else if (choice == 4) deletefirst();
  276.         else if (choice == 5) deleteLast();
  277.         else if (choice == 6)
  278.         {
  279.             printf("Enter the position: ");
  280.             int pos;
  281.             scanf("%d",&pos);
  282.             deleteAt(pos);
  283.         }
  284.         else if(choice == 7)
  285.         {
  286.             printf("Enter the value: ");
  287.             int val;
  288.             scanf("%d",&val);
  289.             search(val);
  290.         }
  291.         else if(choice == 8) printNode(head);
  292.         else if(choice == 9) break;
  293.         else printf("Wrong Choice Input!!\n\n");
  294.     }
  295.     freeTheList();
  296. }
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
Add Comment
Please, Sign In to add comment