Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * [ArrayBasedQueue.cpp]
- * The ArrayBasedQueue class definition/implementation.
- */
- #include "ArrayBasedQueue.h"
- /**
- * ArrayBasedQueue CLASS CONSTRUCTOR(s):
- */
- ArrayBasedQueue::ArrayBasedQueue(int q_max)
- {
- /**
- * NOTE:
- * Whenever an item is added (enqueued) to the queue, it will be added to the end of the array,
- * therefore, the index will be increased by 1, and the item added.
- *
- * Whenever the item is removed (dequeued), it will be removed from the beginning, leaving an
- * empty element at the beginning of the array. Therefore, all the items will be shifted one
- * space back.
- */
- MAX_LENGTH = q_max + 1; // sets the actual maximum length of the queue with the value provided
- q_front = MAX_LENGTH - 1; // sets the front of the queue (at an empty spot)
- q_rear = MAX_LENGTH - 1; // sets the rear of the queue (starting at the front (because the queue is empty))
- pData = new float[MAX_LENGTH]; // creates a dynamic array to use as the queue
- }
- /**
- * ArrayBasedQueue CLASS ACCESSORS:
- */
- void ArrayBasedQueue::Dequeue(float &item)
- {
- /**
- * NOTE:
- * Every time there's a dequeue, the index will be moved to the front (not the items). In this
- * case, the index means the preceding of the actual front.
- */
- q_front = (q_front + 1) % MAX_LENGTH; //
- item = pData[q_front]; // stores the front element in the address of the item provided
- }
- /**
- * ArrayBasedQueue CLASS MUTATORS:
- */
- void ArrayBasedQueue::Enqueue(float item)
- {
- 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)
- pData[q_rear] = item; // adds the element to the end of the queue
- }
- /**
- * ArrayBasedQueue CLASS GENERAL USE FUNCTIONS:
- */
- void ArrayBasedQueue::MakeEmpty(void)
- {
- }
- bool ArrayBasedQueue::IsEmpty(void)
- {
- /**
- * NOTE:
- * If the front is at the rear, because of it's circular structure, the queue is empty.
- */
- return(q_rear == q_front);
- }
- bool ArrayBasedQueue::IsFull(void)
- {
- /**
- * NOTE:
- * To check if the queue is full, the only place left in the array to add an element is the
- * reserved one because, whenever elements are being added to an array, the front reaching the
- * rear is an indication that the array is full.
- *
- * Because of the queue's circular structure, there might be empty elements at the beginning of
- * the array, and front can be equal to rear. For this reason, the conventional comparison of
- * the front to the rear will not be implemented.
- */
- 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
- }
- /**
- * ArrayBasedQueue CLASS DESTRUCTOR:
- */
- ArrayBasedQueue::~ArrayBasedQueue()
- {
- delete[] pData; // removes the queue from memory
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement