splash365

singly_linked_list (string)

Jan 11th, 2021 (edited)
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.29 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct Node
  5. {
  6.     string 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.         string val;
  17.         cin >> 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(string 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(string 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,string 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(string 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.             cout << val << ' ';
  194.             printf("found at position: %d\n",index);
  195.             return;
  196.         }
  197.         cur = cur -> next;
  198.         index++;
  199.     }
  200.     cout << val << ' ';
  201.     printf("is not found in the list\n");
  202. }
  203.  
  204. void printNode(struct Node* cur)
  205. {
  206.     printf("Linked List: ");
  207.     while(cur != NULL)
  208.     {
  209.         cout << cur->data << ' ';
  210.         cur = cur -> next;
  211.     }
  212.     printf("\n\n");
  213. }
  214.  
  215. void freeTheList()
  216. {
  217.     struct Node *curNode = head;
  218.     while(head != NULL)
  219.     {
  220.         curNode = head;
  221.         head = head->next;
  222.         free(curNode);
  223.     }
  224. }
  225.  
  226. int main()
  227. {
  228.     int n;
  229.     printf("Enter the number of elements in the list: ");
  230.     scanf("%d",&n);
  231.     printf("Enter %d elements: ", n);
  232.     CreatAList(n);
  233.     printNode(head);
  234.     printf("A linked list has been created!\n\n\n");
  235.  
  236.     while(1)
  237.     {
  238.         printf("1. Insert First\n");
  239.         printf("2. Insert Last\n");
  240.         printf("3. Insert At\n");
  241.         printf("4. Delete First\n");
  242.         printf("5. Delete Last\n");
  243.         printf("6. Delete At\n");
  244.         printf("7. Search\n");
  245.         printf("8. Print List\n");
  246.         printf("9. Exit\n");
  247.         printf("Enter Choice: ");
  248.         int choice;
  249.         scanf("%d",&choice);
  250.         if(choice == 1)
  251.         {
  252.             printf("Enter the element: ");
  253.             string val;
  254.             cin >> val;
  255.             insertFirst(val);
  256.             printf("Successfully inserted!\n\n");
  257.         }
  258.         else if(choice == 2)
  259.         {
  260.             printf("Enter the element: ");
  261.             string val;
  262.             cin >> val;
  263.             insertLast(val);
  264.             printf("Successfully inserted!\n\n");
  265.         }
  266.         else if(choice == 3)
  267.         {
  268.             printf("Enter the position (between first and last): ");
  269.             int pos;
  270.             scanf("%d",&pos);
  271.             printf("Enter the element: ");
  272.             string val;
  273.             cin >> val;
  274.             insertAt(pos,val);
  275.             printf("Successfully inserted!\n\n");
  276.         }
  277.         else if (choice == 4) deletefirst();
  278.         else if (choice == 5) deleteLast();
  279.         else if (choice == 6)
  280.         {
  281.             printf("Enter the position: ");
  282.             int pos;
  283.             scanf("%d",&pos);
  284.             deleteAt(pos);
  285.         }
  286.         else if(choice == 7)
  287.         {
  288.             printf("Enter the value: ");
  289.             string val;
  290.             cin >> val;
  291.             search(val);
  292.         }
  293.         else if(choice == 8) printNode(head);
  294.         else if(choice == 9) break;
  295.         else printf("Wrong Choice Input!!\n\n");
  296.     }
  297.     freeTheList();
  298. }
Add Comment
Please, Sign In to add comment