简体   繁体   中英

How to break out of an infinite loop in my program

I am having a problem with my program. I am trying to take a stack of numbers put them in a circular linked queue and then back in the stack. The program compiles and run but after it is finished with everything my program goes in an blank infinite loop. I am assuming that I am it is because of the function isQueue.enqueue(temp) part of code because when I removed it, my program no longer goes in the infinite loop. The problem is I dont see what I am doing wrong that will keep it in a infinite loop.

Function

#include <fstream>
#include <iostream>
#include <cassert>
#include "stack.h"
#include "queuei.h"

void stackToqueue(stack &isStack, queue &isQueue)
{
    int temp;
    stack newStack;
    while (!isStack.isEmpty())
    {
        temp = isStack.pop();
        newStack.push(temp);
    }
    newStack.displayStack();
    isStack = newStack;
    isStack.displayStack();

    while (!newStack.isEmpty())
    {
        temp = newStack.pop();
        isQueue.enqueue(temp);//stuck in loop
    }
    isQueue.displayQueue();
    while (!isQueue.isEmpty())
    {
        temp = isQueue.dequeue();
        newStack.push(temp);
    }
    temp = isQueue.dequeue();
    newStack.push(temp);

    isStack = newStack;

}

queue.cpp

#include "queuei.h"

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

bool queue::isEmpty()
{
    if (front == rear)
    {
        return true;

    }
    return false;

}
void queue::enqueue(int element)
{
    nodes *temp = new nodes;
    temp->item = element;
    temp->next = NULL;
    if (front == NULL)
    {
        front = temp;
    }
    else
    {
        rear->next = temp;
    }
    rear = temp;
    rear->next = front;
}
int queue::dequeue()
{
    nodes  *tem = new nodes;
    tem = front;
    if (front == NULL)
    {

        cout << "\nQueue is Emtpty\n";

    }
    else
    {

        front = front->next;

    }
    return tem->item;
}

void queue::dequeue(int remove)
{
    if (front == NULL)
    {

        cout << "\nQueue is Emtpty\n";

    }
    else

    {
        front = front->next;
    }
}


void queue::displayQueue()
{
    nodes *p = new nodes;
    p = front;

    if (front == NULL)
    {
        //queue is empty
    }
    else
    {
        cout << "F ";
        while (p != rear)
        {
            cout << p->item << " ";
            p = p->next;

        }
        cout << p->item;

        cout << " L";

    }

}

Problem is in queue::isEmpty() because it will return true when there is a single node and both front and rear are pointing to same node. Checking if they are Null will solve the problem.

bool queue::isEmpty()
{
    if (front == NULL && rear == NULL)
    {
        return true;
    }
    return false;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM