Advertisement
parthosutradhor

Linked List

Jul 14th, 2023 (edited)
1,235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.51 KB | Source Code | 0 0
  1. #include<bits/stdc++.h>
  2. #include <windows.h>
  3. using namespace std;
  4. typedef struct node Node;
  5.  
  6. typedef struct node {
  7.     int value;
  8.     Node* next;
  9. };
  10.  
  11. Node* create_node(int, Node*);
  12. void push_front(Node*&, int);
  13. void push_back(Node*&, int);
  14. int insert_node(Node*&, int, int);
  15. int pop_front(Node*&);
  16. int pop_back(Node*&);
  17. int delete_node(Node*&, int);
  18. void print_list(Node*);
  19.  
  20. int main()
  21. {
  22.     Node* head = NULL;
  23.     char c, c1;
  24.     int x, i, j, m;
  25.     while (true) {
  26.         cout << "Input '1' to push front" << endl;
  27.         cout << "Input '2' to push back" << endl;
  28.         cout << "Input '3' to insert at any index" << endl;
  29.         cout << "Input '4' to pop front" << endl;
  30.         cout << "Input '5' to pop back" << endl;
  31.         cout << "Input '6' to delete at any index" << endl;
  32.         cout << "Input 'X' to exit from the menu" << endl;
  33.         cout << endl << "Enter your Choice: ";
  34.         cin >> c;
  35.         if (c == '1') {
  36.             cout << endl << "Enter the integer to push front: ";
  37.             cin >> x;
  38.             push_front(head, x);
  39.             cout << endl << "Pushing "<< x <<" at front";
  40.             for (j = 0; j < 5; j++) {
  41.                 Sleep(100);
  42.                 cout << ".";
  43.             }
  44.             cout << endl;
  45.             print_list(head);
  46.         }
  47.         else if (c == '2') {
  48.             cout << endl << "Enter the integer to push back: ";
  49.             cin >> x;
  50.             push_back(head, x);
  51.             cout << endl << "Pushing " << x << " at back";
  52.             for (j = 0; j < 5; j++) {
  53.                 Sleep(100);
  54.                 cout << ".";
  55.             }
  56.             cout << endl;
  57.             print_list(head);
  58.         }
  59.         else if (c == '3') {
  60.             cout << endl << "Enter the integer to insert: ";
  61.             cin >> x;
  62.             cout << endl << "Enter the index position: ";
  63.             cin >> i;
  64.             m = insert_node(head, x, i);
  65.             if (m) {
  66.                 cout << endl << "Inserting " << x << " at index " << i;
  67.                 for (j = 0; j < 5; j++) {
  68.                     Sleep(100);
  69.                     cout << ".";
  70.                 }
  71.             }
  72.             cout << endl;
  73.             print_list(head);
  74.         }
  75.         else if (c == '4') {
  76.             cout << endl << "Are you Sure to pop from front? Enter (Y/N) : ";
  77.             cin >> c1;
  78.             if (c1 == 'y' || c1 == 'Y') {
  79.                 m = pop_front(head);
  80.                 if (m) {
  81.                     cout << endl << "Poping from front";
  82.                     for (j = 0; j < 5; j++) {
  83.                         Sleep(100);
  84.                         cout << ".";
  85.                     }
  86.                 }
  87.                 cout << endl;
  88.             }
  89.             else {
  90.                 cout << endl << "Poping terminating";
  91.                 for (j = 0; j < 5; j++) {
  92.                     Sleep(100);
  93.                     cout << ".";
  94.                 }
  95.                 cout << endl;
  96.             }
  97.             print_list(head);
  98.         }
  99.         else if (c == '5') {
  100.             cout << endl << "Are you Sure to pop from back? Enter (Y/N) : ";
  101.             cin >> c1;
  102.             if (c1 == 'y' || c1 == 'Y') {
  103.                 m = pop_back(head);
  104.                 if (m) {
  105.                     cout << endl << "Poping from back";
  106.                     for (j = 0; j < 5; j++) {
  107.                         Sleep(100);
  108.                         cout << ".";
  109.                     }
  110.                 }
  111.                 cout << endl;
  112.             }
  113.             else {
  114.                 cout << endl << "Poping terminating";
  115.                 for (j = 0; j < 5; j++) {
  116.                     Sleep(100);
  117.                     cout << ".";
  118.                 }
  119.                 cout << endl;
  120.             }
  121.             print_list(head);
  122.         }
  123.         else if (c == '6') {
  124.             cout << endl << "Are you Sure to pop from back? Enter (Y/N) : ";
  125.             cin >> c1;
  126.             if (c1 == 'y' || c1 == 'Y') {
  127.                 cout << endl << "Enter the index to delete from : ";
  128.                 cin >> i;
  129.                 m = delete_node(head, i);
  130.                 if (m) {
  131.                     cout << endl << "Deteling at index " << i;
  132.                     for (j = 0; j < 5; j++) {
  133.                         Sleep(100);
  134.                         cout << ".";
  135.                     }
  136.                 }
  137.                 cout << endl;
  138.             }
  139.             else {
  140.                 cout << endl << "Deletion terminating";
  141.                 for (j = 0; j < 5; j++) {
  142.                     Sleep(100);
  143.                     cout << ".";
  144.                 }
  145.                 cout << endl;
  146.             }
  147.             print_list(head);
  148.         }
  149.         else if (c == 'X') {
  150.             cout << endl << "Exitting";
  151.             for (j = 0; j < 5; j++) {
  152.                 Sleep(300);
  153.                 cout << ".";
  154.             }
  155.             cout << endl;
  156.             break;
  157.         }
  158.         else {
  159.             cout << endl << "Invalid Input. Try again." << endl << endl;
  160.         }
  161.     }
  162.  
  163.     return 0;
  164. }
  165.  
  166. Node* create_node(int value, Node* next) {
  167.     Node* new_node = (Node*)malloc(sizeof(Node));
  168.     if (new_node == NULL) {
  169.         cout << "\nMemory Allocation Failed." << endl;
  170.         return NULL;
  171.     }
  172.     new_node->value = value;
  173.     new_node->next = next;
  174.     return new_node;
  175. }
  176.  
  177. void push_front(Node*& head, int value) {
  178.     Node* new_node = create_node(value, NULL);
  179.     if (new_node == NULL) {
  180.         cout << "\nFailed to pop front." << endl;
  181.         return;
  182.     }
  183.     if (head == NULL) {
  184.         head = new_node;
  185.         return;
  186.     }
  187.     Node* tmp = head;
  188.     head = new_node;
  189.     head->next = tmp;
  190. }
  191.  
  192. void push_back(Node*& head, int value) {
  193.     Node* new_node = create_node(value, NULL);
  194.     if (new_node == NULL) {
  195.         cout << "\nFailed to pop pack." << endl;
  196.         return;
  197.     }
  198.     if (head == NULL) {
  199.         head = new_node;
  200.         return;
  201.     }
  202.     Node* tmp = head;
  203.     while (tmp->next) {
  204.         tmp = tmp->next;
  205.     }
  206.     tmp->next = new_node;
  207. }
  208.  
  209. int insert_node(Node*& head, int value, int index) {
  210.     if (index == 0) {
  211.         push_front(head, value);
  212.         return 1;
  213.     }
  214.     if (head == NULL) {
  215.         cout << "\nHead is NUll. Failed to insert node at index " << index << endl;
  216.         return 0;
  217.     }
  218.     int c = 1;
  219.     Node* tmp = head;
  220.     while (tmp->next && c < index) {
  221.         tmp = tmp->next;
  222.         c++;
  223.     }
  224.     if (c < index) {
  225.         cout << "\nList is too short to insert node at index " << index << endl;
  226.         return 0;
  227.     }
  228.     Node* new_node = create_node(value, NULL);
  229.     if (new_node == NULL) {
  230.         cout << "\nFailed to insert node at index " << index << endl;
  231.         return 0;
  232.     }
  233.     Node* tmp2 = tmp->next;
  234.     tmp->next = new_node;
  235.     new_node->next = tmp2;
  236.     return 1;
  237. }
  238.  
  239. int pop_front(Node*& head) {
  240.     if (head == NULL) {
  241.         cout << "\nList is empty. Failed to pop at front." << endl;
  242.         return 0;
  243.     }
  244.     head = head->next;
  245.     return 1;
  246. }
  247.  
  248. int pop_back(Node*& head) {
  249.     if (head == NULL) {
  250.         cout << "\nList is empty. Failed to pop at end." << endl;
  251.         return 0;
  252.     }
  253.     if (head->next == NULL) {
  254.         head = NULL;
  255.         return 1;
  256.     }
  257.     Node* tmp = head;
  258.     while ((tmp->next)->next) {
  259.         tmp = tmp->next;
  260.     }
  261.     tmp->next = NULL;
  262.     return 1;
  263. }
  264.  
  265. int delete_node(Node*& head, int index) {
  266.     int m;
  267.     if (head == NULL) {
  268.         cout << "\nList is empty. Failed to delete node at index " << index << endl;
  269.         return 0;
  270.     }
  271.     if (index == 0) {
  272.         m = pop_front(head);
  273.         return m;
  274.     }
  275.     Node* tmp = head;
  276.     if (tmp->next == NULL) {
  277.         cout << "\nList is too short to delete node at index " << index << endl;
  278.         return 0;
  279.     }
  280.     int c = 1;
  281.     while ((tmp->next)->next && c < index) {
  282.         tmp = tmp->next;
  283.         c++;
  284.     }
  285.     if (c < index) {
  286.         cout << "\nList is too short to delete node at index " << index << endl;
  287.         return 0;
  288.     }
  289.     tmp->next = (tmp->next)->next;
  290.     return 1;
  291. }
  292.  
  293. void print_list(Node* head) {
  294.     int j;
  295.     cout << endl << "Printing List";
  296.     for (j = 0; j < 3; j++) {
  297.         Sleep(100);
  298.         cout << ".";
  299.     }
  300.     cout << endl;
  301.     while (head) {
  302.         cout << head->value << " ";
  303.         head = head->next;
  304.         Sleep(500);
  305.     }
  306.     cout << endl << endl;
  307. }
  308.  
  309.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement