A Queue implemented as an array in C++


Header file for a queue
//---------------------------------------------------------------
// File: Code131_Queue.h
// Purpose: Header file for a demonstration of a queue implemented 
//        as an array.  Data type: Character
// Programming Language: C++
// Author: Dr. Rick Coleman
//---------------------------------------------------------------
#ifndef CODE131_QUEUE_H
#define CODE131_QUEUE_H

#include <iostream>
using namespace std;

#define MAX_SIZE    25        // Define maximum length of the queue

class Code131_Queue
{
    private:
        int head, tail;           // Index to top of the queue
        char theQueue[MAX_SIZE];  // The queue

    public:
        Code131_Queue();         // Class constructor
        ~Code131_Queue();        // Class destuctor
        void ClearQueue();       // Remove all items from the queue
        bool Enqueue(char ch);   // Enter an item in the queue
        char Dequeue();          // Remove an item from the queue
        bool isEmpty();          // Return true if queue is empty
        bool isFull();           // Return true if queue is full
};

#endif // End of queue header

Implementation (.cpp) file for a queue
//---------------------------------------------------------------
// File: Code131_Queue.cpp
// Purpose: Implementation file for a demonstration of a queue  
//		implemented as an array.    Data type: Character
// Programming Language: C++
// Author: Dr. Rick Coleman
// Date: February 11, 2002
//---------------------------------------------------------------
#include "Code131_Queue.h"


//--------------------------------------------
// Function: Code131_Queue()
// Purpose: Class constructor
// Returns: void
//--------------------------------------------
Code131_Queue::Code131_Queue()
{
	head = tail = -1;
}

//--------------------------------------------
// Function: Code131_Queue()
// Purpose: Class destructor
// Returns: void
//--------------------------------------------
Code131_Queue::~Code131_Queue()
{
	// Nothing to do here since the memory used
	// for the queue is freed automatically.
}

//--------------------------------------------
// Function: ClearQueue()
// Purpose: Remove all items from the queue
// Returns: void
//--------------------------------------------
void Code131_Queue::ClearQueue()
{
	head = tail = -1; // Reset indices to start over
}

//--------------------------------------------
// Function: Enqueue()
// Purpose: Enqueue an item into the queue.
// Returns: true if enqueue was successful
//		or false if the enqueue failed.
// Note: We let head and tail continuing 
//		increasing and use [head % MAX_SIZE] 
//		and [tail % MAX_SIZE] to get the real
//		indices.  This automatically handles
//		wrap-around when the end of the array
//		is reached.
//--------------------------------------------
bool Code131_Queue::Enqueue(char ch)
{
	// Check to see if the Queue is full
	if(isFull()) return false;

	// Increment tail index
	tail++;
	// Add the item to the Queue
	theQueue[tail % MAX_SIZE] = ch;
	return true;
}

//--------------------------------------------
// Function: Dequeue()
// Purpose: Dequeue an item from the Queue.
// Returns: true if dequeue was successful
//		or false if the dequeue failed.
//--------------------------------------------
char Code131_Queue::Dequeue()
{
	char ch;

	// Check for empty Queue
	if(isEmpty()) return '\0';  // Return null character if queue is empty
	else
	{
		head++;
		ch = theQueue[head % MAX_SIZE];		// Get character to return
		return ch;				// Return popped character
	}
}

//--------------------------------------------
// Function: isEmpty()
// Purpose: Return true if the queue is empty
// Returns: true if empty, otherwise false
//--------------------------------------------
bool Code131_Queue::isEmpty()
{
	return (head == tail);
}

//--------------------------------------------
// Function: isFull()
// Purpose: Return true if the queue is full
// Returns: true if full, otherwise false
//--------------------------------------------
bool Code131_Queue::isFull()
{
	// Queue is full if tail has wrapped around
	//	to location of the head.  See note in
	//	Enqueue() function.
	return ((tail - MAX_SIZE) == head);
}

Main file used to test the queue
//---------------------------------------------------------------
// File: QueueMain.c
// Purpose: Main file with tests for a demonstration of a queue 
//        as an array.
// Programming Language: C++
// Author: Dr. Rick Coleman
// Date: February 11, 2002
//---------------------------------------------------------------
#include "Code131_Queue.h"
#include <string.h>

int main(int argc, char **argv)
{
    char            testString[MAX_SIZE + 2];
    int                i;
    char            ch;
    Code131_Queue    *Q;

    cout << "Simple Queue Demonstration\n";
    cout << "(Queue implemented as an Array - Queue data type is character.)\n\n";
    cout << "Creating a queue\n";

    Q = new Code131_Queue();
    cout << "Queue created...\n";

    // Test enqueuing and dequing item on a queue
    cout << "Testing enqueue and dequeue of single item.\n";
    Q->Enqueue('A');
    cout << "Enqueued: " << Q->Dequeue() << "\n";
    cout << "...done\n\n";

    // Test queue by enqueing letters in a string
    strcpy(testString, "abcdefghijklmnopqrasuvwxyz"); 

    // Try to Enqueue all letters in the string
    i = 0;
    cout << "Testing enqueuing of string: " << testString << "\n";
    while(testString[i] != '\0')
    {
        if(!Q->Enqueue(testString[i]))
        {
            cout << "Queue is full. Unable to enqueue " << testString[i] << "\n";
        }
        i++;
    }

    cout << "\n\nDone testing enqueue.\n\nNow testing dequeue.\n";
    // Dequeue letters and print them
    cout << "Dequeued letters are...\n";
    while((ch = Q->Dequeue()) != '\0') // Dequeue returns null terminator 
        cout << ch;            // when queue is empty
    
    cout << "\nEnd of queue encountered...\n";

    cout << "\n\n...done.\n";
    return 0;
}