Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. #include <string>
  2.  
  3. template <typename T>
  4. struct node
  5. {
  6.   T nodeData;
  7.   node<T> *next;
  8.   node<T> *prev;
  9.  
  10.   node(const T& _data, node<T> *nxt = NULL, node<T> *prv = NULL)
  11.   {
  12.     this->nodeData = _data;
  13.     this->next = nxt;
  14.     this->prev = prv;
  15.   }
  16. };
  17.  
  18. template <typename T>
  19. class Queue
  20. {
  21. private:
  22.   node<T> *front;
  23.   node<T> *rear;
  24. public:
  25.   Queue() { front = rear = NULL; }
  26.  
  27.   void enqueue(const T& _input)
  28.   {
  29.     node<T> *tmp = new node<T>(_input);
  30.     if(rear == NULL)
  31.       rear = front = tmp;
  32.     else
  33.       {
  34.     rear->next = tmp;
  35.     tmp->prev = rear;
  36.     rear = tmp;
  37.       }
  38.   }
  39.  
  40.   T dequeue()
  41.   {
  42.     node<T>* tmp = this->front;
  43.     T result;
  44.     if(this->front == NULL)
  45.       {
  46.     std::cout << "\nQueue underflow\n";
  47.     return T();
  48.       }
  49.     else
  50.       {
  51.     if(tmp->next != NULL)
  52.       {
  53.         tmp = tmp->next;
  54.         result = this->front->nodeData;
  55.         delete this->front;
  56.         this->front = tmp;
  57.         return result;
  58.       }
  59.     else
  60.       {
  61.         result = this->front->nodeData;    
  62.         delete this->front;
  63.         this->front = NULL;
  64.         this->rear = NULL;
  65.         return result;
  66.       }
  67.       }
  68.   }
  69.  
  70.   bool isEmpty()
  71.   {
  72.     return this->front == NULL;
  73.   }
  74.  
  75.   // void remove(node<T> *n)
  76.   // {
  77.   //   if(front == NULL || n == NULL)
  78.   //     return;
  79.  
  80.   //   if(front == n)
  81.   //     front = front->next;
  82.  
  83.   //   if(n->next != NULL)
  84.   //     n->next->prev = n->prev;
  85.  
  86.   //   if(n->prev != NULL)
  87.   //     n->prev->next = n->next;
  88.  
  89.   //   delete n;
  90.   // }
  91.  
  92.   void printQueue()
  93.   {
  94.     node<T> *tmp = front;
  95.     while(tmp != NULL)
  96.       {
  97.     std::cout << tmp->nodeData << " ";
  98.     tmp = tmp->next;
  99.       }
  100.   }
  101.  
  102.   void printTail()
  103.   {
  104.     node<T> *tmp = rear;
  105.     while(tmp != NULL)
  106.       {
  107.     std::cout << tmp->nodeData << " ";
  108.     tmp = tmp->prev;
  109.       }
  110.   }
  111.  
  112.   node<T>* getHead() { return this->front; }
  113.   node<T>* getTail() { return this->rear; }
  114. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement