Advertisement
Phr0zen_Penguin

ArrayBasedQueue.cpp - VTC C++ Array-Based Queue

Apr 29th, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.86 KB | None | 0 0
  1. /**
  2.  * [ArrayBasedQueue.cpp]
  3.  * The ArrayBasedQueue class definition/implementation.
  4.  */
  5.  
  6. #include "ArrayBasedQueue.h"
  7.  
  8. /**
  9.  * ArrayBasedQueue CLASS CONSTRUCTOR(s):
  10.  */
  11. ArrayBasedQueue::ArrayBasedQueue(int q_max)
  12. {
  13.     /**
  14.      * NOTE:
  15.      * Whenever an item is added (enqueued) to the queue, it will be added to the end of the array,
  16.      * therefore, the index will be increased by 1, and the item added.
  17.      *
  18.      * Whenever the item is removed (dequeued), it will be removed from the beginning, leaving an
  19.      * empty element at the beginning of the array.  Therefore, all the items will be shifted one
  20.      * space back.
  21.      */
  22.  
  23.     MAX_LENGTH = q_max + 1;         // sets the actual maximum length of the queue with the value provided
  24.     q_front = MAX_LENGTH - 1;       // sets the front of the queue (at an empty spot)
  25.     q_rear = MAX_LENGTH - 1;        // sets the rear of the queue (starting at the front (because the queue is empty))
  26.     pData = new float[MAX_LENGTH];  // creates a dynamic array to use as the queue
  27. }
  28.  
  29.  
  30. /**
  31.  * ArrayBasedQueue CLASS ACCESSORS:
  32.  */
  33. void ArrayBasedQueue::Dequeue(float &item)
  34. {
  35.     /**
  36.      * NOTE:
  37.      * Every time there's a dequeue, the index will be moved to the front (not the items).  In this
  38.      * case, the index means the preceding of the actual front.
  39.      */
  40.     q_front = (q_front + 1) % MAX_LENGTH;   //
  41.     item = pData[q_front];                  // stores the front element in the address of the item provided
  42. }
  43.  
  44.  
  45. /**
  46.  * ArrayBasedQueue CLASS MUTATORS:
  47.  */
  48. void ArrayBasedQueue::Enqueue(float item)
  49. {
  50.     q_rear = (q_rear + 1) % MAX_LENGTH; // after the end of the queue is reached, the preceding expression takes the program back to its beginning again (in a circular fashion)
  51.     pData[q_rear] = item;               // adds the element to the end of the queue
  52. }
  53.  
  54.  
  55. /**
  56.  * ArrayBasedQueue CLASS GENERAL USE FUNCTIONS:
  57.  */
  58. void ArrayBasedQueue::MakeEmpty(void)
  59. {
  60.  
  61. }
  62.  
  63. bool ArrayBasedQueue::IsEmpty(void)
  64. {
  65.     /**
  66.      * NOTE:
  67.      * If the front is at the rear, because of it's circular structure, the queue is empty.
  68.      */
  69.     return(q_rear == q_front);
  70. }
  71.  
  72. bool ArrayBasedQueue::IsFull(void)
  73. {
  74.     /**
  75.      * NOTE:
  76.      * To check if the queue is full, the only place left in the array to add an element is the
  77.      * reserved one because, whenever elements are being added to an array, the front reaching the
  78.      * rear is an indication that the array is full.
  79.      *
  80.      * Because of the queue's circular structure, there might be empty elements at the beginning of
  81.      * the array, and front can be equal to rear.  For this reason, the conventional comparison of
  82.      * the front to the rear will not be implemented.
  83.      */
  84.     return((q_rear + 1) % MAX_LENGTH == q_front);   // the remaining (empty) elements in the array are compared with the front to check if the queue is full
  85. }
  86.  
  87.  
  88. /**
  89.  * ArrayBasedQueue CLASS DESTRUCTOR:
  90.  */
  91. ArrayBasedQueue::~ArrayBasedQueue()
  92. {
  93.     delete[] pData; // removes the queue from memory
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement