bleonard989 0 Newbie Poster

I am having problems with my linked queue adt. I don't get any error messages, but it just isn't doing anything in the menu driven client code. It's just to test the queue to make sure it works correctly. The most problem I'm having is with my print function. If someone could help me with that, I will probably be able to figure out the rest. I am new to queues, so any advice would help.

queue.h - header file

// Header file for Queue ADT - Linked
// Template class for dynamically allocated queue - rear pointer tracked only
// Nodes for queue are allocated and deallocated as needed
#ifndef QUEUE_H
#define QUEUE_H
#include <iostream>
using namespace std;

#include<fstream>
using std::ostream;

#include <new>
using std::bad_alloc;


class FullQueue                                 // FullQueue output of errors
{};

class EmptyQueue                                // EmptyQueue output of errors
{};


// DYNAMICALLY LINKED IMPLEMENTATION OF QUEUE

template <class ItemType>       // record type for nodes on queue
struct NodeType
{
    ItemType info;
    NodeType* next;
};

template <class ItemType>
class QueueType
{
    public:

        QueueType();
        QueueType(const QueueType &);  // Copy constructor
        ~QueueType();                // Class destructor.

        void MakeEmpty();              // Initializes the queue to an empty state
        bool IsEmpty() const;            // Determines whether the queue is empty.
        bool IsFull() const;               // Determines whether the queue is full.

        void Enqueue(ItemType newItem);    // Adds newItem to the rear of the queue.
        void Dequeue(ItemType & item);     // Removes front item from the queue and
        // returns it in the parameter.

        ostream & Print(ostream & stream);            // Prints contents of queue to screen

           QueueType& operator= (const QueueType &);     // Function: operator=

    private:
        NodeType<ItemType>* front;      // pointer to front node
        NodeType<ItemType>* rear;       // pointer to rear node
};




//-----------------------------------------------------------------
// Constuctor
// Pre: nothing has been initialized
// Post: both pointers will be initialized
template <class ItemType>
QueueType<ItemType>::QueueType()
{
        front = NULL;
        rear = NULL;
}


//-----------------------------------------------------------------
// Function: MakeEmpty()
// Pre: "right" queue has been initialized
// Post: Queue is empty
template <class ItemType>
void QueueType<ItemType>::MakeEmpty()
{
        NodeType<ItemType>* tempPtr;

        while (front != NULL)
        {
                tempPtr = front;
                front = front->next;
                delete tempPtr;
        }

        rear = NULL;
}


//-----------------------------------------------------------------
// Copy Constuctor
// Pre: "right" queue has been initialized
// Post: Queue contains all elements contained in "right" queue
template <class ItemType>
QueueType<ItemType>::QueueType(const QueueType<ItemType> & right)
{

        if (right.front != NULL)
        {
                NodeType<ItemType> * rightPtr = right.front;
                rear = front =  NULL;

                do
                {
                        Enqueue (rightPtr->info);
                        rightPtr = rightPtr->next;
                }
                while (rightPtr !=NULL);
        }
        else
                rear = front = NULL;
}


//-----------------------------------------------------------------
// Deconstuctor
// Pre: MakeEmpty function initialized
// Post: Deconstructor initialized
template <class ItemType>
QueueType<ItemType>::~QueueType()
{
        MakeEmpty();
}


//-----------------------------------------------------------------
// Function: IsFull - // Returns true if there is no room for another ItemType
//  on the free store; false otherwise.
// Pre: Queue has been initialized
// Post: Queue is not modified
template<class ItemType>
bool QueueType<ItemType>::IsFull() const
{
        NodeType<ItemType>* location;
        try
        {
                location = new NodeType<ItemType>;
                delete location;

                return false;
        }
        catch(bad_alloc &memoryAllocationException)
        {
                return true;
        }
}



//-----------------------------------------------------------------
// Function: IsEmpty - // Returns true if empty
// Pre: Queue has been initialized
// Post: Queue is not modified
template <class ItemType>
bool QueueType<ItemType>::IsEmpty() const
{
        return (front == NULL);
}


//-----------------------------------------------------------------
// Function: Enqueue - // Adds new item to rear of the queue
// Pre: Queue has been initialized
// Post: If (queue is not full).  newItem is at the rear of queue
//       If not, a FullQueue exception is thrown
template <class ItemType>
void QueueType<ItemType>::Enqueue(ItemType newItem)
{
        if (IsFull())
                throw FullQueue();
        else
        {
                NodeType<ItemType>* newNode;

                newNode = new NodeType<ItemType>;
                newNode->info = newItem;
                newNode->next = NULL;

                if (rear == NULL)
                        front = newNode;
                else
                        rear->next = newNode;
                rear = newNode;
        }
}


//-----------------------------------------------------------------
// Function: Dequeue - // Removes front item from the queue and returns it
// Pre: Queue has been initialized
// Post: If (queue is not empty) front of queue is removed and a copy returned
//       else EmptyQueue exception is thrown
template <class ItemType>
void QueueType<ItemType>::Dequeue(ItemType& item)
{
        if (IsEmpty())
                throw EmptyQueue();
        else
        {
                NodeType<ItemType>* tempPtr;

                tempPtr = front;
                item = front->info;
                front = front->next;

                if (front == NULL)
                        rear = NULL;

                delete tempPtr;
        }
}


//-----------------------------------------------------------------
// Function: Print - // Prints Results of queue
// Pre:
// Post: Cout << statetment of queue contants
template <class ItemType>
ostream & QueueType<ItemType>::Print(ostream & stream)
{
        NodeType<ItemType>* tempPtr = front;


        stream << "The queue contains: " << endl;
        while (tempPtr != rear)
        {
                stream << tempPtr->info << " ";
                tempPtr = tempPtr->next;
        }
        return stream;
}

#endif

queueTest.cpp - client code

// Menu driven test program

#include <iostream>
#include "queue.h"
using namespace std;

int main()
{
        int num, item;
        QueueType<int> queue;
 while (num != 0)                        // an input of 0 will end program
        {
                cout << endl;
                cout << "Please select from the following options:" << endl;
                cout << '\t' << "1  - Add an item" << endl;
                cout << '\t' << "2  - Remove an item" << endl;
                cout << '\t' << "3  - Determine if stack is full" << endl;
                cout << '\t' << "4  - Determine if stack is empty" << endl;
                cout << '\t' << "5  - Clear all items from stack" << endl;
                cout << '\t' << "6  - Print" << endl;
                cout << '\t' << "7  - Copy" << endl;
                cout << '\t' << "0  - Quit program" << endl;
                cout << endl;
                cout << '\t' << "Selection: ";
                        cin >> num;
                cout << endl;

                if (num == 1)
                {
                        cout << "Enter an integer to add: ";
                        cin >> item;
                        try
                        {
                                queue.Enqueue(item);
                                cout << endl << item << " successfully added" << endl;
                        }
                        catch (FullQueue exceptionObject)       // error message
                        {
                                cerr << endl << "Queue is full" << endl;
                        }

                }

                else if (num == 2)
                {
                        try
                        {
                                queue.Dequeue (item);
                                cout << endl << item << " successfully deleted" << endl;
                        }
                        catch (EmptyQueue exceptionObject)
                        {
                                cerr << endl << "Queue is empty" << endl;
                        }
                }

                else if (num == 3)
                {
                        if (queue.IsFull() == true)
                        cout << "Queue is full" << endl;
                        else
                        cout << "Queue is not full" << endl;
                }

                else if (num == 4)
                {
                        if (queue.IsEmpty() == true)
                        cout << "Queue is empty" << endl;
                        else
                        cout << "Queue is not empty" << endl;
                }

                else if (num == 5)
                {
                        queue.MakeEmpty();
                        cout << "Queue is now cleared";
                }

                else if (num == 6)
                {

                        queue.Print(cout);
                        cout << endl;
                }

                else if (num == 7)
                {
                        cout << "Old Queue" << endl;

                        cout << "New Queue" << endl;
                }

                else if (num < 0 || num > 7)
                {
                        cout << "Invalid input, please try again" << endl;
                }
}
return 0;
}

I also don't understand how to implement the copy constructor in the client code either...

Any help is greatly appreciated

~Ben

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.