关于堆栈为空的链接列表堆栈问题

时间:2018-11-19 23:26:47

标签: c++ linked-list stack

我正在研究一个链表列表栈和一系列函数。我目前不了解的是为什么我的“ isEmpty”功能无法正常工作。我相信我的写作方式很有道理。本质上,如果Front为Null,则列表必须为空,这意味着“ isEmpty”将返回false。我遇到的问题是我的程序说该列表始终为空,无论它实际上是否为空。我不确定是什么问题。任何帮助将不胜感激。

struct node
{
    int data;
    node *next;
}*front = NULL, *rear = NULL, *p = NULL, *np = NULL;
void push(int x)
{
    np = new node;
    np->data = x;
    np->next = NULL;
    if(front == NULL)
    {
        front = rear = np;
        rear->next = NULL;
    }
    else
    {
        rear->next = np;
        rear = np;
        rear->next = NULL;
    }
}
int pop()
{
    int x;
    if (front == NULL)
    {
        cout<<"empty queue\n";
    }
    else
    {
        p = front;
        x = p->data;
        front = front->next;
        delete(p);
        return(x);
    }
}

int peek()
{
    int x;
    if (front == NULL)
    {
        cout<<"empty queue\n";
    }
    else
    {
        p = front;
        x = p->data;
        front = front->next;
        return(x);
    }
}

bool isEmpty()
{
    if (front == NULL)
    {
        return true;
    }
    else if (front != NULL)
    {
        return false;
    }
}

void Display()
{
cout << front;
}

int main()
{
    int n, c = 0, x;
    bool is_empty = isEmpty();
    cout<<"Enter the number of values to be pushed into queue\n";
    cin>>n;
    while (c < n)
    {
    cout<<"Enter the value to be entered into queue\n";
    cin>>x;
        push(x);
        c++;
    }
    cout<<endl<<"Pop value: ";

    if (front != NULL)
            cout<<pop()<<endl;

    cout<<endl<<"Peak value: ";

    if (front != NULL)
            cout<<peek()<<endl;

    if (is_empty == true)
    {
        cout<<endl<<"The list is empty";
    }
    else if (is_empty == false)
    {
        cout<<endl<<"The list is not empty";
    }


    cout << endl << "The current contents of the stack are: ";
while(front != NULL)
{
    Display();
    if(front == NULL)
        cout << "The stack is empty";
    break;
}
    getch();
}

1 个答案:

答案 0 :(得分:0)

您的代码存在一些问题。

您已经定义了isEmpty()函数,但实际上不是。在填充列表之前,您已经声明了一个单独的变量is_empty,将其设置为返回值isEmpty(),并且在对列表进行任何更改后再也不会更新is_empty。这就是为什么您的代码始终报告列表为“空”的原因,即使实际上并非如此。您需要摆脱is_empty变量,并在每次需要检查空值时调用isEmpty()

此外,如果peek()为NULL,则pop()front的返回值是不确定的

然后peek()从列表中弹出front节点。只有pop()可以这样做。

并且pop()不检查rear是否指向正在弹出的节点。在这种情况下,您没有将rear重置为NULL。

在退出程序之前,您不会释放列表中剩余的任何节点。

请尝试以下类似操作:

#include <iostream>
#include <limits>

struct node
{
    int data;
    node *next;

    node(int value): data(value), next(NULL) {}
};

node *front = NULL;
node *rear = NULL;

void push(int x)
{
    node **np = (rear) ? &(rear->next) : &front;
    *np = new node(x);
    rear = *np;
}

int peek()
{
    if (front)
        return front->data;
    return -1;
}

int pop()
{
    int x = peek();

    if (front)
    {
        node *p = front;
        front = front->next;
        if (p == rear) rear = NULL;
        delete p;
    }

    return x;
}

bool isEmpty()
{
    return !front;
}

void clear()
{
    while (front)
    {
        node *p = front;
        front = front->next;
        delete p;
    }
    rear = NULL;
}

void display()
{
    node *n = front;
    if (n)
    {
        cout << n->data;
        while (n = n->next)
            cout << ' ' << n->data;
    }
}

int askForNumber(const char *prompt)
{
    int n;
    cout << prompt << ": ";
    cin >> n;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    return n;
}

int main()
{
    int n, x;

    n = askForNumber("Enter the number of values to be pushed into queue");

    for(int c = 0; c < n; ++c)
    {
        x = askForNumber("Enter the value to be entered into queue");
        push(x);
    }
    cout << endl;

    cout << "Pop value: ";
    if (!isEmpty())
        cout << pop();
    else
        cout << "empty queue";
    cout << endl;

    cout << "Peak value: ";
    if (!isEmpty())
        cout << peek();
    else
        cout << "empty queue";
    cout << endl;

    if (isEmpty())
        cout << "The list is empty";
    else
        cout << "The list is not empty";
    cout << endl;

    cout << "The current contents of the stack are:" << endl;
    display();
    cout << endl;

    clear();

    cin.get();
    return 0;
}