Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot dequeue a node in C++

Tags:

c++

My query: why does my program come up with a run-time error when dequeuing (actually, deleting) the only node I've enqueued in the queue? I've written some debugging statements in the appropriate function, which points out that there is something wrong with the line

delete front;

When the program executes the line, it wants to quit because it does not respond to me. I tried

if(front) delete front;

but to no avail. I tried to find an answer on Google but did not come up with any satisfactory results. This is very strange. I've been dealing with OOP for a few years now, but that's a first for me. Here is my code:

================================ Queue.cpp =======================================

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

Queue::Queue()
{
    QueueNode * front = NULL;
    QueueNode * rear = NULL;
}

Queue::~Queue()
{
    while(rear) dequeue();
}

void Queue::enqueue(int row, int col)
{
    // create the child state:
    QueueNode * newNode;
    newNode = new QueueNode;

    // write in child state's coordinates:
    newNode->row = row;
    newNode->col = col;

    // enqueue the child state:
    if(!front) // if empty, new is front
    {
        // first node = front and back
        front = newNode;
        rear = newNode;
    }
    else // not the first node:
    {
        newNode->next = rear; // new points to back
        rear = newNode; // new is the back
    }
}

void Queue::dequeue()
{
    cout << "\nHere\n";
    delete front;
    cout << "\nHere 2\n";
    front = rear;
    cout << "\nHere 3\n";
    while(front->next) front = front->next;
    cout << "\nHere 4\n";
}

================================= Queue.h =======================================

#ifndef QUEUE_H
#define QUEUE_H

class Queue
{
    public:
        struct QueueNode
        {
            // numbers:
            int row;
            int col;

            QueueNode * next;
        };

        QueueNode * front;
        QueueNode * rear;

        Queue();
        ~Queue();

        void enqueue(int, int);
        void dequeue();
        //void traverse(); // scan for repetition of location.
        //void displayQueue() const;
};

#endif

Note:

1) I did not include the code for the main driver because that would take a lot of substitution of tabs for 4 spaces.

2) I've enqueued only one queue node.

3) I've made everything public in the queue class because I was desperate in finding out what the problem is. I'm going to keep it that way for the time being.

4) This is my first time asking a question on StackOverflow.com, so if I did something wrong, then I'm still learning.

5) I'm using Visual C++ 2008 Express Edition.

Again, my query: why does the program come up with a run-time error when deleting the only node in the queue?

like image 257
Andrei Avatar asked Sep 29 '13 07:09

Andrei


1 Answers

Error is here

Queue::Queue()
{
    QueueNode * front = NULL;
    QueueNode * rear = NULL;
}

should be

Queue::Queue()
{
    front = NULL;
    rear = NULL;
}

In your version you have two local variables in your constructor that just happen to have the same names as the variables in your class. So your constructor is not initialising your class at all.

BTW you should get into the habit of using initialiser lists

Queue::Queue() : front(NULL), rear(NULL)
{
}

if only beause this mistake can't happen

BTW it's a well asked question.

like image 175
john Avatar answered Sep 20 '22 21:09

john