A Queue implemented as a linked structure in C++.


Header file for a queue
//---------------------------------------------------------------
// File: Code137_Queue.h
// Purpose: Header file for a demonstration of a queue implemented 
//        as a linked structure.  Data type: Character
// Programming Language: C++
// Author: Dr. Rick Coleman
//---------------------------------------------------------------
#ifndef CODE137_QUEUE_H
#define CODE137_QUEUE_H

#include <iostream>
using namespace std;

struct QNode
{
    char  ch;
    QNode *next;
};

class Code137_Queue
{
    private:
        QNode *head, *tail;         // Pointers to head and tail of the queue

    public:
        Code137_Queue();            // Class constructor
        ~Code137_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: Code137_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 "Code137_Queue.h"


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

//--------------------------------------------
// Function: Code137_Queue()
// Purpose: Class destructor
// Returns: void
//--------------------------------------------
Code137_Queue::~Code137_Queue()
{
    ClearQueue();
}

//--------------------------------------------
// Function: ClearQueue()
// Purpose: Remove all items from the queue
// Returns: void
//--------------------------------------------
void Code137_Queue::ClearQueue()
{
    QNode *temp;
    while(head != NULL)
    {
        temp = head;
        head = head->next;
        delete temp;
    }

    head = tail = NULL; // 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.
//--------------------------------------------
bool Code137_Queue::Enqueue(char ch)
{
    QNode *temp;

    // Check to see if the Queue is full
    if(isFull()) return false;

    // Create new node for the queue
    temp = new QNode ();
    temp->ch = ch;
    temp->next = NULL;

    // Check for inserting first in the queue
    if(head == NULL)
        head = tail = temp;
    else
    {
        tail->next = temp; // Insert into the queue
        tail = temp;       // Set tail to new last node
    }

    return true;
}

//--------------------------------------------
// Function: Dequeue()
// Purpose: Dequeue an item from the Queue.
// Returns: Character being dequeued or '\0'
//    if dequeue failed.
//--------------------------------------------
char Code137_Queue::Dequeue()
{
    char ch;
    QNode *temp;

    // Check for empty Queue
    if(isEmpty()) return '\0';  // Return null character if queue is empty
    else
    {
        ch = head->ch;        // Get character to return
        temp = head;
        head = head->next;    // Advance head pointer
        delete temp;            // Free old head
        // Check to see if queue is empty
        if(isEmpty())
        {
            head = tail = NULL;        // Reset everything to empty queue
        }
    }
    return ch;                // Return popped character
}

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

//--------------------------------------------
// Function: isFull()
// Purpose: Return true if the queue is full
// Returns: true if full, otherwise false
//--------------------------------------------
bool Code137_Queue::isFull()
{
    return false;
}

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

int main(int argc, char **argv)
{
    char            testString[27];
    int                i;
    char            ch;
    Code137_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 Code137_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;
}