Write a class named RingBufferQueue
that represents a queue of integers implemented using a "ring buffer" array.
A Ring Buffer Queue, or RBQ, is implemented by using an underlying array. In our implementation, the
capacity is capped; once the array is full, additional elements cannot be added until something is dequeued.
Another "interesting" thing about RBQs is that we don't want to shift elements when an element is enqueued or dequeued. Instead, we want to keep track of the front and tail of the Queue.
For example, say our queue can hold 5 elements and we enqueue 3 elements: 10, 20, 30.
Our queue would look like this:
index 0 1 2 3 4
+----+----+----+----+----+
value | 10 | 20 | 30 | | |
+----+----+----+----+----+
^ ^
| |
head tail
If we enqueued two more elements of 40 and 50, our queue would then be full:
index 0 1 2 3 4
+----+----+----+----+----+
value | 10 | 20 | 30 | 40 | 50 |
+----+----+----+----+----+
^ ^
| |
head tail
At this point, we cannot add any additional elements until we dequeue at least one element.
Dequeuing will remove the element at head, and head will move onto the next element.
If we dequeue 2 elements, our queue will look like this:
index 0 1 2 3 4
+----+----+----+----+----+
value | | | 30 | 40 | 50 |
+----+----+----+----+----+
^ ^
| |
head tail
Now there's room to add more elements!
Since we still don't want to shift any elements, adding an additional element will wrap around.
So if we enqueue an element, our queue will look like this:
index 0 1 2 3 4
+----+----+----+----+----+
value | 60 | | 30 | 40 | 50 |
+----+----+----+----+----+
^ ^
| |
tail head
Notice that the tail's index is less than the head's index!
Your job is to implement a RingBufferQueue
class.
Your class should have the following public members:
member name |
description |
RingBufferQueue() |
constructs an empty queue with a capacity of 5 |
~RingBufferQueue() |
frees array memory associated with the queue |
enqueue(value) |
enqueues elem if the queue has room; throws an error if queue is full |
dequeue() |
returns and removes the element at the front of the queue; throws a string exception if queue is empty |
peek() |
Returns element at the front of the queue; throws a string exception if queue is empty |
isEmpty() |
Returns true if queue is empty and false otherwise |
isFull() |
Returns true if queue is full and false otherwise |
size() |
Returns the number of elements in the queue |
operator << |
outputs a string representation of the queue from front to back, such as: {10, 20, 30} |
You should define the entire class including the class heading, the private member variables, and the declarations and definitions of all the public member functions and constructor.