SHARE
TWEET

queue cpp

a guest Oct 21st, 2019 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Node {
  5.   struct Node *next;
  6.   struct Node *prev;
  7.   string data;
  8.   Node(string dataIn) { data = dataIn; next = nullptr; prev = nullptr;}
  9. };
  10. class LinkedQueue {
  11. public:
  12.   int size;
  13.   struct Node *head = nullptr;
  14.   struct Node *tail = nullptr;
  15.   LinkedQueue() {
  16.     size = 0;
  17.   };
  18.   void enqueue(string newData) {
  19.     if (head == nullptr) {
  20.       Node *new_node = new Node(newData);
  21.       head = new_node;
  22.       size+=1;
  23.       return;
  24.     }
  25.     else if (tail == nullptr && head != nullptr) { //second node in queue
  26.       Node *new_node = new Node(newData);
  27.       new_node->prev = head;
  28.       head->next = new_node;
  29.       tail = new_node;
  30.       size+=1;
  31.       return;
  32.     }
  33.     Node *new_node = new Node(newData);
  34.     tail->next = new_node;
  35.     new_node->prev = tail;
  36.     tail = new_node;
  37.     size+=1;
  38.     cout << "success" << endl;
  39.   }
  40.   Node* search(string value) {
  41.     if (head == nullptr) {
  42.       cout << "There are no items in the queue" << endl;
  43.       return nullptr;
  44.     }
  45.     Node *ptr = head;
  46.     while (ptr !=nullptr) {
  47.       if (ptr->data == value) {
  48.         return ptr;
  49.       }
  50.       ptr = ptr->next;
  51.     }
  52.     return nullptr;
  53.   }
  54.   void dequeue(Node *refNode) {
  55.     if (refNode->next == nullptr) { //tail
  56.       if (refNode == tail) { //check
  57.         Node *ptr = tail;
  58.         (ptr->prev)->next = nullptr;
  59.         tail = ptr->prev;
  60.         delete ptr;
  61.       }
  62.     }
  63.     else if (refNode->prev == nullptr) { //head
  64.       if (refNode == head) { //check
  65.         Node *ptr = head;
  66.         (ptr->next)->prev = nullptr;
  67.         head = ptr->next;
  68.         delete ptr;
  69.       }
  70.     }
  71.     (refNode->prev)->next = (refNode->next);
  72.     (refNode->next)->prev = (refNode->prev);
  73.     delete refNode;
  74.   }
  75.   void printQueue() {
  76.     if (head == nullptr) {
  77.       cout << "There are no items in the queue" << endl;
  78.       return;
  79.     } else {
  80.       Node *ptr = head;
  81.       while (ptr !=nullptr) {
  82.         cout << ptr->data << endl;
  83.         ptr = ptr->next;
  84.       }
  85.     }
  86.   }
  87. };
  88. int main() {
  89.   LinkedQueue queue;
  90.   queue.enqueue("Timmy");
  91.   queue.enqueue("Jimmy");
  92.   queue.enqueue("Kimmy");
  93.   queue.enqueue("Qimmy");
  94.   queue.enqueue("Bimmy");
  95.   queue.dequeue(queue.search("Qimmy"));
  96.   cout << queue.size << endl;
  97.   cout << "Head value: " << queue.head->data << endl;
  98.   cout << "Tail value: " << queue.tail->data << endl;
  99.   queue.printQueue();
  100.   return 0;
  101. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top