Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- struct Node {
- Node *prev;
- Node *next;
- string data;
- Node(string dat) {
- this->data = dat;
- this->next = nullptr;
- this->prev = nullptr;
- }
- };
- class DeQue {
- private:
- // head is not needed in a CDLL / DE Queue
- // front and rear will do the job
- // Node *head;
- Node *front;
- Node *rear;
- public:
- DeQue() {
- // head = nullptr;
- front = nullptr;
- rear = nullptr;
- }
- void frontInsert(string dat) {
- Node *newNode = new Node(dat);
- if (front == nullptr) {
- front = rear = newNode;
- newNode->next = newNode;
- newNode->prev = newNode;
- } else {
- // SOME NICE DOCUMENTATION :P
- // Make the newly inserted node's next point to the current first node
- // ok this is a DE queue so we dont need a head pointer.
- // the front poiints to the first element and rear points to the last element.
- // newNode->next = head;
- newNode->next = front;
- // Make the newly inserted node point to the last node in the LL
- // correct logik when currently there are more than 1 nodes in the LL
- // newNode->prev = head -> does not maintain the correct structure for circular LL
- // newNode->prev = head->prev;
- newNode->prev = rear;
- // Make the current first node's previous point to the newly inserted Node
- // head->prev = newNode;
- front->prev = newNode;
- // Make the last node point to the newly inserted node
- // head->prev->next = newNode;
- rear->next = newNode;
- // Now update the head to point to the newly inserted Node
- // head = newNode;
- front = newNode;
- // END DOCUMENTATION
- }
- }
- void rearInsert(string dat) {
- Node *newNode = new Node(dat);
- if (head == nullptr) {
- front = newNode;
- newNode->next = newNode;
- newNode->prev = newNode;
- } else {
- // front and rear ftw!
- newNode->next = front;
- newNode->prev = rear;
- rear->next = newNode;
- front->prev = newNode;
- rear = newNode;
- }
- }
- void display() {
- if (front == nullptr) {
- cout << "Queuwue is Empty" << endl;
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment