/* File: queues.cpp
 *
 * Implements the abstract queue as a circular array.
 */

#include "queues.h"                 // the interface for the queue class

/*
 * Queue::remove() - removes the queue head and returns the value.
 *                   An exception is thrown if the queue is empty.
 */
int Queue::remove()
{
    int value;                      // the value popped 

    if (empty)                      // if the queue is empty ...
        throw Queue_empty();

    value = data[head];             // otherwise remove the queue head by
    head = (head + 1) % queue_size; // incrementing the head index
    if (head == tail)
        empty = true;               // if head == tail, the queue is empty

    return (value);
}

/*
 * Queue::peek() - return the element at the head of the queue.
 *                 An exception is thrown if the queue is empty.
 */
int Queue::peek()
{
    if (empty)                      // if the queue is empty ...
        throw Queue_empty();
    return (data[head]);            // otherwise, get the head element
}

/*
 * Queue::insert() - takes an element and adds it to the queue.
 *                   if there is no room on the queue, an exception is thrown
 */
void Queue::insert(int element)
{
    if (!empty && (head == tail))
        throw Queue_full();             // the queue is full

    data[tail] = element;               // otherwise, add the element
    tail = (tail + 1) % queue_size;     // and increment the queue tail index
    empty = false;                      // the queue is no longer empty
}
 

