开发者

LinkedList/Stack/Queue - Help with Dequeuing

I had to write a linked list, then turn it into a dynamic Stack, then turn that into a dynamic Queue. Well everything seems to work except the "dequeuing", right as the programs about to finish, it gives me an error: "An unhandled win32 exception occured in LinkedList_Stack_BNS11.exe [4972].".

I'm only assuming it's the dequeuing, because as I step through and/or run the program, it runs smoothly up till that part, so maybe I sent one of the pointers wrong or something?

Output:

Enquing 5 items.... // Finsihes

The values in the queue were (Dequeuing):

0

1

2

// Correct number in que but...

//Program gives that error right here. When it should finish and close.

If I included too much code let me know and I'll chop it down to just开发者_StackOverflow the "Dequeuing" (Which is in the very middle of all the stuff below)

Thanks in advance for the help!! I'm just not seeing what I did wrong. Thinking it maybe has something to do with where "head" is pointing? Idk.

Header File:

class NumberList
{
private:
    //
    struct ListNode
    {
        int value;  // Value in this node
        struct ListNode *next; // Pointer to the next node
    };

    ListNode *head; // List head pointer
    ListNode *rear;

public:
    //Constructor
    NumberList()
    { head = NULL; rear = NULL; }

    //Destructor
    ~NumberList();

    //Stack operations
    bool isEmpty();

    //Queue operations
    void enqueue(int);
    void dequeue(int &);
};
#endif

List_Stack_Queue.cpp:

bool NumberList::isEmpty()
{
    bool status;

    if(!head)
        status = true;
    else
        status = false;

    return status;
}

     void NumberList::enqueue(int num)
    {
        ListNode *newNode; // Point to a new node

        // Allocate a new node and store num there.
        newNode = new ListNode;
        newNode->value = num;

        //If there are no nodes in the list
        // make newNode the first node.
        if(isEmpty())
        {
            head = newNode;
            rear = head;
            //newNode->next = NULL;
        }
        else
        {
            rear->next = newNode;
            rear = rear->next;
            //newNode->next = head;
            //head = newNode;
        }
    }

    void NumberList::dequeue(int &num)
    {
        ListNode *temp;

        if(isEmpty())
            cout << "The queue is empty.\n";
        else
        {
            num = head->value;
            temp = head;
            head = head->next;
            delete temp;
        }
    }

MAIN:

const int MAX_VALUES = 3;

// Create a DynIntQueue object.
NumberList iQueue;

// Enqueue a series of numbers.
cout << "Enqueuing " << MAX_VALUES << " items...\n";
for (int x = 0; x < MAX_VALUES; x++)
    iQueue.enqueue(x);

cout << endl;

//Dequeue and retrieve all numbers in the queue
cout << "The values in the queue were (Dequeuing):\n";
while(!iQueue.isEmpty())
{
    int value;
    iQueue.dequeue(value);
    cout << value << endl;
} 
return 0;


Last nodes's next element should be set to NULL in a linked list. So,

void NumberList::enqueue(int num)
{
    // ...
    if(isEmpty())
    {
        head = newNode;
        head->next = NULL;
        rear = head;
    }
    else
    {
        rear->next = newNode;
        rear = rear->next;
        rear->next = NULL;     // Pointing the next node element to null.
    }
}

To, me it seems some thing is wrong with Numberlist::isEmpty(); member function. How you are deciding whether the list is empty or not ? Show the definition of it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜