Advertisement
Phr0zen_Penguin

LinkedQueue.cpp - VTC C++ Linked Queue

May 3rd, 2015
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.84 KB | None | 0 0
  1. /**
  2.  * [LinkedQueue.cpp]
  3.  * The LinkedQueue class definition/implementation.
  4.  */
  5.  
  6. #include "LinkedQueue.h"
  7.  
  8. /**
  9.  * LinkedQueue CLASS CONSTRUCTOR(s):
  10.  */
  11. LinkedQueue::LinkedQueue()
  12. {
  13.     q_front = 0;
  14.     q_rear = 0;
  15. }
  16.  
  17.  
  18. /**
  19.  * LinkedQueue CLASS ACCESSORS:
  20.  */
  21. void LinkedQueue::Dequeue(float &item)
  22. {
  23.     Node    *pTemp;             // a temporary node to store the item taken from the beginning of the queue
  24.  
  25.     pTemp = q_front;            // point the temporary pointer to the front of the queue
  26.     item = q_front->element;    // store the element of the front of the queue into the address of the provided variable
  27.  
  28.     q_front = q_front->next;    // points the current front of the queue to the next node of itself
  29.  
  30.         if(q_front == 0)    // if the new front is a null pointer...
  31.             q_rear = 0;         // ...set the rear of the queue as a null pointer
  32.  
  33.     delete pTemp;               // delete the temporary pointer (containing the dequeued item)
  34. }
  35.  
  36.  
  37. /**
  38.  * LinkedQueue CLASS MUTATORS:
  39.  */
  40. void LinkedQueue::Enqueue(float item)
  41. {
  42.     Node    *newNode = new Node();
  43.  
  44.     newNode->element = item;
  45.     newNode->next = 0;          // because new items are always added to the end of the queue
  46.  
  47.         if(q_rear == 0) // if the queue is empty...
  48.             q_front = newNode;          // ...simply add the new item to the front of the queue
  49.         else            // ...
  50.             q_rear->next = newNode;     // ...add the item to the rear of the queue
  51.  
  52.     q_rear = newNode;   // point the rear of the queue to the new node making it the new rear of the queue
  53. }
  54.  
  55.  
  56. /**
  57.  * LinkedQueue CLASS GENERAL USE FUNCTIONS:
  58.  */
  59. void LinkedQueue::MakeEmpty(void)
  60. {
  61.     // what if the queue is already empty?
  62.     // what if an unnecessary pointer should not be created?
  63.     // what if unnecessary CPU resources should not be used?
  64.     if(!IsEmpty())
  65.     {
  66.         Node    *pTemp; // what is the memory allocation failed in an attempt to make empty a full queue?
  67.  
  68.             while(q_front != 0)         // while items are still in the queue
  69.             {
  70.                 pTemp = q_front;            // store the front in the temporary pointer/node
  71.                 q_front = q_front->next;    // move the front to the next item (making it the new front)
  72.  
  73.                 delete pTemp;               // delete the temporary pointer (containing the previous front that it pointed to)
  74.             }
  75.  
  76.         q_rear = 0;                         // nullify the rear if the queue (it points to nothing)
  77.     }
  78. }
  79.  
  80. bool LinkedQueue::IsEmpty(void)
  81. {
  82.     return(q_front == 0);               // if the front of the queue points to nothing then the queue is empty
  83. }
  84.  
  85. bool LinkedQueue::IsFull(void)
  86. {
  87.     Node    *pTemp = new Node();        // try to allocate memory for a temporary node
  88.  
  89.         if(pTemp == 0)              // if the attempted memory allocation failed...
  90.             return true;                // ...true is returned, indicating a full queue
  91.         else                        // ...
  92.         {
  93.             delete pTemp;               // remove the temporary pointer
  94.  
  95.             return false;           // ...false is returned
  96.         }
  97. }
  98.  
  99.  
  100. /**
  101.  * LinkedQueue CLASS DESTRUCTOR:
  102.  */
  103. LinkedQueue::~LinkedQueue()
  104. {
  105.     MakeEmpty();
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement