#include "IntegerArrayQueue.h"

// enqueue: if there is space available enqueue value and
// return true, otherwise return false
bool IntegerArrayQueue::enqueue(int value) {

    // If back index is two positions before the front index
    // (queue is full), return false
    if ((back + 2) % size == front) {

        return false;

    }

    // If back index is one less than size
    if ((back + 1) == size) {

        // Wrap back index back around to front
        back = 0;

    } else {

        // Increment back index
        back++;

    }

    // Set the value in queue at back index to the value passed to function
    array[back] = value;

    return true;

}

// dequeue: if there is a value at the front of the queue
// return the value and remove from the queue,
// otherwise return 0
int IntegerArrayQueue::dequeue() {

    // If back index is one position before front
    // (queue is empty), return 0
    if ((back + 1) % size == front) {

        return 0;

    }

    // Store value being dequeued
    int dequeuedValue = array[front];

    // If front index is one less than size
    if ((front + 1) == size) {

        // Wrap front index back around to front
        front = 0;

    } else {

        // Increment front index
        front++;

    }

    // Return dequeued value
    return dequeuedValue;

}