0

I have an assignment and it's to build a queue linked list.

Our professor gave us the skeleton and told us to use his main.

I wrote my functions and everything compiled right but when I ran it, I got this error.

enter image description here

Don't know what to do from here.

Source Code:

                #include<iostream>
    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 ;
            }

            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 >= 5);
    }

    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)
    {
        NodeType<ItemType>* tempPtr;

        if(!IsEmpty())
        {
        tempPtr = front;
        x = front->info;
        front = front->next;
        if(front == NULL) 
        {
            rear = NULL;
        }
        delete tempPtr;
        }

        size--;
    }

    template<class ItemType>
    void Queue<ItemType>::Print()
    {
        NodeType<ItemType> *temp;
        temp = rear;
        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.Dequeue(x);
        IntQueue.Enqueue(10);
        IntQueue.Enqueue(20);
        IntQueue.Enqueue(30);
        IntQueue.Enqueue(40);
        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<float>FloatQueue;
        float y;
        FloatQueue.MakeEmpty();
        FloatQueue.Dequeue(y);
        FloatQueue.Enqueue(7.1);
        cout << "float length 3 = " << FloatQueue.length() << endl;
        FloatQueue.Enqueue(2.3);
        cout << "float length 4 = " << FloatQueue.length() << endl;
        FloatQueue.Enqueue(3.1);
        FloatQueue.Dequeue(y);
        cout << "The float queue contains:  " << endl;
        FloatQueue.Print();
        Queue<float> FloatQueue2 = FloatQueue;
        cout << "The float queue 2 contains:  " << endl;
        FloatQueue2.Print();
        FloatQueue.MakeEmpty();
        cout << "The float queue 3 contains:  " << endl;
        FloatQueue2.Print();

        system("pause");
        return 0;
    }

The problem I'm having is obviously with the print function.

Any help is appreciated.

1 Answer 1

1

Inside your copy constructor, you aren't setting rear to anything when x.front == NULL. This sets temp to an invalid value inside Print, causing you both to loop when you shouldn't and dereference an invalid pointer.

Sign up to request clarification or add additional context in comments.

3 Comments

I just did a quick fix in my print and did temp = front.. it's just that it doesn't print anything now, so I'm guessing I'm getting the printing function wrong. Any idea with that?
Your print is fine. You need a rear = NULL in your copy constructor after front = NULL. In C++ pointer problems often error in a different function than their cause.
I understand and took your suggestions and added rear = NULL but for some reason my print function isn't printing anything out, so I'm puzzled.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.