The text below is selected, press Ctrl+C to copy to your clipboard. (⌘+C on Mac) No line numbers will be copied.
Guest
Doublylinkedlist
By Guest on 11th December 2023 06:21:30 AM | Syntax: TEXT | Views: 99



New Paste New paste | Download Paste Download | Toggle Line Numbers Show/Hide line no. | Copy Paste Copy text to clipboard
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Node {
  6.   public:
  7.     int key;
  8.   int data;
  9.   Node * next;
  10.   Node * previous;
  11.  
  12.   Node() {
  13.     key = 0;
  14.     data = 0;
  15.     next = NULL;
  16.     previous = NULL;
  17.   }
  18.   Node(int k, int d) {
  19.     key = k;
  20.     data = d;
  21.   }
  22. };
  23.  
  24. class DoublyLinkedList {
  25.  
  26.   public:
  27.     Node * head;
  28.  
  29.   DoublyLinkedList() {
  30.     head = NULL;
  31.   }
  32.   DoublyLinkedList(Node * n) {
  33.     head = n;
  34.   }
  35.  
  36.   // 1. CHeck if node exists using key value
  37.  
  38.   Node * nodeExists(int k) {
  39.     Node * temp = NULL;
  40.     Node * ptr = head;
  41.  
  42.     while (ptr != NULL) {
  43.       if (ptr -> key == k) {
  44.         temp = ptr;
  45.       }
  46.       ptr = ptr -> next;
  47.     }
  48.  
  49.     return temp;
  50.   }
  51.  
  52.   // 2. Append a node to the list
  53.  
  54.   void appendNode(Node * n) {
  55.     if (nodeExists(n -> key) != NULL) {
  56.       cout << "Node Already exists with key value : " << n -> key << ". Append another node with different Key value" << endl;
  57.     } else {
  58.       if (head == NULL) {
  59.         head = n;
  60.         cout << "Node Appended as Head Node" << endl;
  61.       } else {
  62.         Node * ptr = head;
  63.         while (ptr -> next != NULL) {
  64.           ptr = ptr -> next;
  65.         }
  66.         ptr -> next = n;
  67.         n -> previous = ptr;
  68.         cout << "Node Appended" << endl;
  69.       }
  70.     }
  71.   }
  72.  
  73.   // 3. Prepend Node - Attach a node at the start
  74.   void prependNode(Node * n) {
  75.     if (nodeExists(n -> key) != NULL) {
  76.       cout << "Node Already exists with key value : " << n -> key << ". Append another node with different Key value" << endl;
  77.     } else {
  78.       if (head == NULL) {
  79.         head = n;
  80.         cout << "Node Prepended as Head Node" << endl;
  81.       } else {
  82.         head -> previous = n;
  83.         n -> next = head;
  84.         head = n;
  85.         cout << "Node Prepended" << endl;
  86.       }
  87.  
  88.     }
  89.   }
  90.  
  91.   // 4. Insert a Node after a particular node in the list
  92.   void insertNodeAfter(int k, Node * n) {
  93.     Node * ptr = nodeExists(k);
  94.     if (ptr == NULL) {
  95.       cout << "No node exists with key value: " << k << endl;
  96.     } else {
  97.       if (nodeExists(n -> key) != NULL) {
  98.         cout << "Node Already exists with key value : " << n -> key << ". Append another node with different Key value" << endl;
  99.       } else {
  100.         Node * nextNode = ptr -> next;
  101.         // inserting at the end
  102.         if (nextNode == NULL) {
  103.           ptr -> next = n;
  104.           n -> previous = ptr;
  105.           cout << "Node Inserted at the END" << endl;
  106.         }
  107.  
  108.         //inserting in between
  109.         else {
  110.           n -> next = nextNode;
  111.           nextNode -> previous = n;
  112.           n -> previous = ptr;
  113.           ptr -> next = n;
  114.  
  115.           cout << "Node Inserted in Between" << endl;
  116.  
  117.         }
  118.  
  119.       }
  120.     }
  121.   }
  122.  
  123.   // 5. Delete node by unique key. Basically De-Link not delete
  124.   void deleteNodeByKey(int k) {
  125.     Node * ptr = nodeExists(k);
  126.     if (ptr == NULL) {
  127.       cout << "No node exists with key value: " << k << endl;
  128.     } else {
  129.  
  130.       if (head -> key == k) {
  131.         head = head -> next;
  132.         cout << "Node UNLINKED with keys value : " << k << endl;
  133.       } else {
  134.         Node * nextNode = ptr -> next;
  135.         Node * prevNode = ptr -> previous;
  136.         // deleting at the end
  137.         if (nextNode == NULL) {
  138.  
  139.           prevNode -> next = NULL;
  140.           cout << "Node Deleted at the END" << endl;
  141.         }
  142.  
  143.         //deleting in between
  144.         else {
  145.           prevNode -> next = nextNode;
  146.           nextNode -> previous = prevNode;
  147.           cout << "Node Deleted in Between" << endl;
  148.  
  149.         }
  150.       }
  151.     }
  152.   }
  153.  
  154.  
  155.   // 6th printing
  156.   void printList() {
  157.     if (head == NULL) {
  158.       cout << "No Nodes in Doubly Linked List";
  159.     } else {
  160.       cout << endl << "Doubly Linked List Values : ";
  161.       Node * temp = head;
  162.  
  163.       while (temp != NULL) {
  164.         cout << "(" << temp -> key << "," << temp -> data << ") <--> ";
  165.         temp = temp -> next;
  166.       }
  167.     }
  168.  
  169.   }
  170.  
  171. };
  172.  
  173. int main() {
  174.  
  175.   DoublyLinkedList obj;
  176.   int option;
  177.   int key1, k1, data1;
  178.   do {
  179.     cout << "\nWhat operation do you want to perform? Select Option number. Enter 0 to exit." << endl;
  180.     cout << "1. appendNode()" << endl;
  181.     cout << "2. prependNode()" << endl;
  182.     cout << "3. insertNodeAfter()" << endl;
  183.     cout << "4. deleteNodeByKey()" << endl;
  184.     cout << "5. print()" << endl;
  185.     cout << "6. Clear Screen" << endl << endl;
  186.  
  187.     cin >> option;
  188.     Node * n1 = new Node();
  189.     //Node n1;
  190.  
  191.     switch (option) {
  192.     case 0:
  193.       break;
  194.     case 1:
  195.       cout << "Append Node Operation \nEnter key & data of the Node to be Appended" << endl;
  196.       cin >> key1;
  197.       cin >> data1;
  198.       n1 -> key = key1;
  199.       n1 -> data = data1;
  200.       obj.appendNode(n1);
  201.       //cout<<n1.key<<" = "<<n1.data<<endl;
  202.       break;
  203.  
  204.     case 2:
  205.       cout << "Prepend Node Operation \nEnter key & data of the Node to be Prepended" << endl;
  206.       cin >> key1;
  207.       cin >> data1;
  208.       n1 -> key = key1;
  209.       n1 -> data = data1;
  210.       obj.prependNode(n1);
  211.       break;
  212.  
  213.     case 3:
  214.       cout << "Insert Node After Operation \nEnter key of existing Node after which you want to Insert this New node: " << endl;
  215.       cin >> k1;
  216.       cout << "Enter key & data of the New Node first: " << endl;
  217.       cin >> key1;
  218.       cin >> data1;
  219.       n1 -> key = key1;
  220.       n1 -> data = data1;
  221.  
  222.       obj.insertNodeAfter(k1, n1);
  223.       break;
  224.  
  225.     case 4:
  226.  
  227.       cout << "Delete Node By Key Operation - \nEnter key of the Node to be deleted: " << endl;
  228.       cin >> k1;
  229.       obj.deleteNodeByKey(k1);
  230.  
  231.       break;
  232.     case 5:
  233.       obj.printList();
  234.  
  235.       break;
  236.     case 6:
  237.       system("cls");
  238.       break;
  239.     default:
  240.       cout << "Enter Proper Option number " << endl;
  241.     }
  242.  
  243.   } while (option != 0);
  244.  
  245.   return 0;
  246. }