sidrs

[TODO] FDS (LV) Ass 7 - DE Queue using CDLL

Sep 30th, 2024 (edited)
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. struct Node {
  6.     Node *prev;
  7.     Node *next;
  8.     string data;
  9.  
  10.     Node(string dat) {
  11.         this->data = dat;
  12.         this->next = nullptr;
  13.         this->prev = nullptr;
  14.     }
  15. };
  16.  
  17. class DeQue {
  18. private:
  19.     // head is not needed in a CDLL / DE Queue
  20.     // front and rear will do the job
  21.     // Node *head;
  22.     Node *front;
  23.     Node *rear;
  24.  
  25. public:
  26.     DeQue() {
  27.         // head = nullptr;
  28.         front = nullptr;
  29.         rear = nullptr;
  30.     }
  31.  
  32.     void frontInsert(string dat) {
  33.         Node *newNode = new Node(dat);
  34.  
  35.         if (front == nullptr) {
  36.             front = rear = newNode;
  37.             newNode->next = newNode;
  38.             newNode->prev = newNode;
  39.         } else {
  40.             // SOME NICE DOCUMENTATION :P
  41.             // Make the newly inserted node's next point to the current first node
  42.             // ok this is a DE queue so we dont need a head pointer.
  43.             // the front poiints to the first element and rear points to the last element.
  44.             // newNode->next = head;
  45.             newNode->next = front;
  46.             // Make the newly inserted node point to the last node in the LL
  47.             // correct logik when currently there are more than 1 nodes in the LL
  48.             // newNode->prev = head -> does not maintain the correct structure for circular LL
  49.             // newNode->prev = head->prev;
  50.             newNode->prev = rear;
  51.             // Make the current first node's previous point to the newly inserted Node 
  52.             // head->prev = newNode;
  53.             front->prev = newNode;
  54.             // Make the last node point to the newly inserted node
  55.             // head->prev->next = newNode;
  56.             rear->next = newNode;
  57.             // Now update the head to point to the newly inserted Node
  58.             // head = newNode;
  59.             front = newNode;
  60.             // END DOCUMENTATION
  61.         }
  62.     }
  63.  
  64.     void rearInsert(string dat) {
  65.         Node *newNode = new Node(dat);
  66.         if (head == nullptr) {
  67.             front = newNode;
  68.             newNode->next = newNode;
  69.             newNode->prev = newNode;
  70.         } else {
  71.             // front and rear ftw! 
  72.             newNode->next = front;
  73.             newNode->prev = rear;
  74.             rear->next = newNode;
  75.             front->prev = newNode;
  76.             rear = newNode;
  77.         }
  78.     }
  79.  
  80.     void display() {
  81.         if (front == nullptr) {
  82.             cout << "Queuwue is Empty" << endl;
  83.         }
  84.  
  85.     }
  86.  
  87.  
  88. };
  89.  
Advertisement
Add Comment
Please, Sign In to add comment