#include "queue.h"

class Queue<ValueType>

This class models a linear structure called a queue in which values are added at one end and removed from the other. This discipline gives rise to a first-in/first-out behavior (FIFO) that is the defining feature of queues. The fundamental queue operations are enqueue (add to back) and dequeue (remove from front). Both of these operations run in O(1) time.

Queues do not allow access to elements by index, nor can you iterate through the elements using the range-based for loop.
Constructor
Queue()  O(1) Initializes a new empty queue.
Methods
clear()  O(1) Removes all elements from the queue.
dequeue()  O(1) Removes and returns the frontmost element in the queue.
enqueue(value)  O(1) Adds value to the back of the queue.
equals(q)  O(N) Returns true if the two queues contain the same elements in the same order.
isEmpty()  O(1) Returns true if the queue contains no elements.
peek()  O(1) Returns the frontmost element in the queue, without removing it.
size()  O(1) Returns the number of values in the queue.
toString()  O(N) Returns a printable string representation of this queue.
Operators
queue1 == queue1  O(N) Returns true if queue1 and queue2 contain the same elements.
queue1 != queue2  O(N) Returns true if queue1 and queue2 are different.
ostream << queue  O(N) Outputs the contents of the queue to the given output stream.
istream >> queue  O(N) Reads the contents of the given input stream into the queue.

Constructor detail


Queue();
Initializes a new empty queue. You may also optionally provide an initializer list of values. The newly created queue will contain those values listed left-to-right in order from front of the queue to the back.

Usage:

Queue<ValueType> queue;
Queue<ValueType> queue = { frontValue, middleValue, backValue };

Method detail


void clear();
Removes all elements from the queue.

Usage:

queue.clear();

ValueType dequeue();
Removes and returns the frontmost element in the queue. If the queue is empty, this function signals an error.

Usage:

ValueType first = queue.dequeue();

void enqueue(ValueType value);
Adds value to the back of the queue.

Usage:

queue.enqueue(value);

bool equals(const Queue& queue) const;
Returns true if the two queues contain exactly the same values in the same order. Identical in behavior to the == operator.

Usage:

if (queue1.equals(queue2)) ...

bool isEmpty() const;
Returns true if the queue contains no elements.

Usage:

if (queue.isEmpty()) ...

ValueType peek() const;
Returns the frontmost element in the queue, without removing it. If the queue is empty, this function signals an error.

Usage:

ValueType first = queue.peek();

int size() const;
Returns the number of values in the queue.

Usage:

int n = queue.size();

string toString() const;
Returns a printable string representation of this queue. The elements are listed left-to-right from the front of the queue to the back, such as "{value1, value2, value3}".

Usage:

string str = queue.toString();

ostream& operator<<(const Queue& queue);
Outputs the contents of queue to the given output stream. The output is in the form {value1, value2, value3} where elements are listed left-to-right from the front of the queue to the back.

Usage:

cout << queue << endl;

Documented: Fall Quarter 2020


istream& operator>>(Queue& queue);
Reads the contents of the given input stream into queue. Any previous contents of the queue are replaced. The input is expected to be in the form {value1, value2, value3} where elements are listed left-to-right from the front of the queue to the back. If unable to read a proper queue from the stream, the operation results in a stream fail state.

Usage:

if (infile >> queue) ...

Documented: Fall Quarter 2020