hi

so why isnt queue printing anything where it should?

#include<iostream>
#include<string>

using namespace std;

template<class ItemType>
struct NodeType
{
    ItemType info;
    NodeType* next;
};

template<class ItemType>
class Queue
{
private:
    int size;
    NodeType<ItemType>* front;  // It points to the front of a singly-linked list
    NodeType<ItemType>* rear;   // It points to the end of a singly-linked list

public:
    Queue();                    // default constructor: Queue is created and empty
    Queue(const Queue<ItemType> &x);  // copy constructor: implicitly called 
    // for a deep copy
    void MakeEmpty();    // Queue is made empty; you should deallocate all 
    //  the nodes of the linked list
    bool IsEmpty();   // test if the queue is empty
    bool IsFull();   // test if the queue is full; assume MAXITEM=5
    int length();    // return the number of elements in the queue
    void Print();   // print the value of all elements in the queue in the sequence 
    // from the front to rear
    void Enqueue(ItemType x);   // insert x to the rear of the queue  
    // Precondition: the queue is not full
    void Dequeue(ItemType &x);  // delete the element from the front of the queue
    // Precondition: the queue is not empty
    ~Queue();  // Destructor:  memory for the dynamic array needs to be deallocated
};

template<class ItemType>
Queue<ItemType>::Queue()
{
    size = 0;
    front = NULL;
    rear = NULL;
};

template<class ItemType>
Queue<ItemType>::Queue(const Queue<ItemType> &x) 
{
    NodeType<ItemType>* ptr1;
    NodeType<ItemType>* ptr2;

    if (x.front == NULL)
    {
        front = NULL;
        rear = NULL;
    }

    else // allocate memory for first node
    {
        front = new NodeType<ItemType>;
        front->info = x.front->info;
        ptr1 = x.front->next;
        ptr2 = front;
        while (ptr1 != NULL) // deep copy other nodes
        {
            ptr2->next = new NodeType<ItemType>;
            ptr2 = ptr2->next;
            ptr2->info = ptr1->info;
            ptr1 = ptr1->next;
        }

        ptr2->next = NULL;
        rear = ptr2;
    }
};

template<class ItemType>
void Queue<ItemType>::MakeEmpty()
{
    NodeType<ItemType>* tempPtr;
    while (front != NULL)
    {
        tempPtr = front;
        front = front->next;
        delete tempPtr;
    }
    rear = NULL;
}

template<class ItemType>
bool Queue<ItemType>::IsEmpty()
{
    return (size == 0);
};

template<class ItemType>
bool Queue<ItemType>::IsFull()
{
    return (size >= 100);
};

template<class ItemType>
int Queue<ItemType>::length()
{
    return size;
};

template<class ItemType>
void Queue<ItemType>::Enqueue(ItemType x)
{
    NodeType<ItemType>* newNode;
    newNode = new NodeType<ItemType>;

    newNode->info = x;
    newNode->next = NULL;

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

    size++;
};

template<class ItemType>
void Queue<ItemType>::Dequeue(ItemType &x) // removes item from the rear of the Queue // Decrements size;
{
    struct NodeType<ItemType>* temp = front;

    if (!IsEmpty())
    {
        if (front == NULL)
        {
            cout << "The Queue is empty." << endl;
            return;
        }
        if (front == rear)
            front = rear = NULL;
        else
            front = front->next;
        free(temp);
    }

    size--;
};

template<class ItemType>
void Queue<ItemType>::Print()
{
    NodeType<ItemType> *temp;
    temp = front;
    while (temp != NULL)
    {
        cout << temp->info << " " << endl;
        temp = temp->next;
    }
};

template<class ItemType>
Queue<ItemType>::~Queue()
{
    MakeEmpty();
}

int main()
{
    Queue<int>IntQueue;
    int x;
    IntQueue.MakeEmpty();
    IntQueue.Enqueue(2);
    IntQueue.Enqueue(4);
    IntQueue.Enqueue(6);
    IntQueue.Enqueue(8);
    IntQueue.Dequeue(x);
    cout << "int length 3 = " << IntQueue.length() << endl;
    IntQueue.Dequeue(x);
    cout << "int length 4 = " << IntQueue.length() << endl;
    cout << "The int queue contains : " << endl;
    IntQueue.Print();
    if (IntQueue.IsFull() == false)
        cout << "The int queue is not full !" << endl;
    else
        cout << "The int queue is full !" << endl;

    Queue<string>StringQueue;
    string y;
    StringQueue.MakeEmpty();
    StringQueue.Dequeue(y);
    StringQueue.Enqueue("John");
    cout << "string length 3 = " << StringQueue.length() << endl;
    StringQueue.Enqueue("Brown");
    cout << "string length 4 = " << StringQueue.length() << endl;
    StringQueue.Enqueue("Betty");
    StringQueue.Dequeue(y);
    cout << "The string queue contains : " << endl;
    StringQueue.Print();
    Queue<string> StringQueue2 = StringQueue;
    cout << "The string queue 2 contains:  " << endl;
    StringQueue2.Print();
    StringQueue.MakeEmpty();
    cout << "The string queue 3 contains:  " << endl;
    StringQueue2.Print();

    system("pause");
    return 0;
}

here is image if you cant run it

Recommended Answers

All 2 Replies

You have a few small problems but the one causing the error you are seeing is in Queue<ItemType>::Enqueue, if rear is NULL you set front but you do not set rear so the next time you enter this function rear is still NULL and you just overwrite front, incidentally causing a memory leak, and not actually setting up a queue.

Other errors

size is not set in the copy constructor

In Queue<ItemType>::Dequeue you don't set x

You have multiple unnecessary ; after every function definition.

And finally: in Queue<ItemType>::Dequeue you decrement size regardless of if you actually deleted an item from the queue which means if called on an empty queue it goes negative and gets out of step with the actual queue size.

got it before the reply, i used stepwise debugging until i figured something wrong with rear. though thank u :)

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.