SHARE
TWEET

Untitled

a guest Nov 19th, 2019 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "Q_Node.h"
  2.  
  3. Q_Node::Q_Node() : element(nullptr), next(nullptr) {};
  4.  
  5. Q_Node::~Q_Node()
  6. {
  7.     next = nullptr;
  8. }
  9.  
  10. void Q_Node::set_element(Node* element)
  11. {
  12.     this->element = element;
  13. }
  14.  
  15. Node* Q_Node::get_element()
  16. {
  17.     return element;
  18. }
  19.  
  20. void Q_Node::set_next(Q_Node* pointer)
  21. {
  22.     next = pointer;
  23. }
  24.  
  25. Q_Node* Q_Node::get_next()
  26. {
  27.     return next;
  28. }
  29.  
  30. #include "Q_Node.h"
  31. #include <stdexcept>
  32. using namespace std;
  33.  
  34. class Queue
  35. {
  36. private:
  37.     int size;
  38.     Q_Node* first , * last;
  39. public:
  40.     Queue();
  41.  
  42.     ~Queue();
  43.  
  44.     // Adds an element in the end of the queue
  45.     void enqueue(Node*);
  46.     // Extracts the first element of the queue and returns its data
  47.     Node* dequeue();
  48.     //Returns data information of the first element
  49.     Node* peek();
  50.     //Returns the size of the queue
  51.     int get_size();
  52. };
  53.  
  54. Queue::Queue() : size(0), first(NULL), last(NULL) {};
  55.  
  56. Queue::~Queue()
  57. {
  58.     while (first)
  59.     {
  60.         last = first;
  61.         first = first->get_next();
  62.         delete last;
  63.     }
  64. }
  65.  
  66. void Queue::enqueue(Node* element)
  67. {
  68.     size++;
  69.     Q_Node* temp = new Q_Node;
  70.     temp->set_element(element);
  71.     if (first == NULL)
  72.     {
  73.         first = last = temp;
  74.     }
  75.     else
  76.     {
  77.         last->set_next(temp);
  78.         last = temp;
  79.     }
  80. }
  81.  
  82. Node* Queue::dequeue()
  83. {
  84.     if (first == NULL)
  85.         throw out_of_range("The queue is empty");
  86.     size--;
  87.     Q_Node* temp = first;
  88.     first = first->get_next();
  89.     Node* element = temp->get_element();
  90.     delete temp;
  91.     return element;
  92. }
  93.  
  94. Node* Queue::peek()
  95. {
  96.     if (first == NULL)
  97.         throw out_of_range("The queue is empty");
  98.     return first->get_element();
  99. }
  100.  
  101. int Queue::get_size()
  102. {
  103.     return size;
  104. }
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