A queue is a collection data type in which elements are removed in the order that they were inserted (with the first being inserted being removed first). Queues define two main operations:
Additionally, a peek operation can be implemented which returns the first value added without removing it.
template <class T>
class Queue {
public:
Queue();
// Insert into the back of the queue
void enqueue(const T& element);
// Remove and return from the front of the queue
T dequeue();
}
Using a singly linked list with head and tail pointer the front of the queue. New elements are inserted at the tail of the linked list and elements are removed from the head to ensure
A circular array can be used to create a queue by maintaining an integer that stores the index of the front and the back of the queue.
When elements of the queue are removed instead of having to move all the elements in the array backwards one index (an
If the queue's max size is unknown the circular array can be combined with a resizable array to ensure the queue can grow as necessary, otherwise a fixed size can be decided with older elements being overwritten as required.