C++ 双链表堆栈删除函数不工作

C++ 双链表堆栈删除函数不工作,c++,linked-list,stack,doubly-linked-list,C++,Linked List,Stack,Doubly Linked List,我已经实现了双链表来在堆栈中推送和弹出值。我的pop函数不起作用。我已经做了几次这个程序的试运行,所有这些都是书面的。如果可能,请引导 #include<iostream> #include<conio.h> #include<iomanip> using namespace std; struct node { node * prev; int val; node

我已经实现了双链表来在堆栈中推送和弹出值。我的pop函数不起作用。我已经做了几次这个程序的试运行,所有这些都是书面的。如果可能,请引导

 #include<iostream>
    #include<conio.h>
    #include<iomanip>

    using namespace std;

    struct node
    {
        node * prev;
        int val;
        node * next;    
    };

    class myStack
    {
        node * first;
        node * cur;
        node * prev0;

        public:
            myStack (): first(NULL), cur(NULL), prev0(NULL){}

            void push();

            void pop();

            void noutput();

            void output();

            ~myStack(){}
    };

    void myStack :: push()
    {
        cur = new node;
        cur->prev = NULL;
        cur->next = NULL;
        cout<<"Enter the Number to push in Stack :"<<endl;
        cin>>cur->val;
        cout<<endl;

        if(first == NULL)
        {
            first = prev0 = cur;
        }

        else
        {
            prev0->next = cur;
            cur->prev = prev0;
            prev0 = cur;

        }
    }

    void myStack :: pop()
    {       
        prev0 = cur->prev;
        delete cur;
        prev0->next = NULL;
        cur = prev0;
    }

    void myStack :: noutput()
    {
            cur = first;

            system("cls");
            cout<<setw(70)<<"NODE VIEW"<<endl;
            cout<<setw(55)<<"Prev"<<"        Cur"<<"       Next";
            cout<<endl<<endl;

            while(cur)
            {
                cout<<setw(55)<<cur->prev<<" | "<<cur<<" | "<<cur->next<<endl;
                cur = cur->next;
            }
            system("pause");
    }

    void myStack :: output()
    {
            cur = first;

            system("cls");

            cout<<setw(60)<<"STACK VIEW"<<endl<<endl;

            while(cur)
            {
                cout<<setw(55)<<" | "<<cur->val<<endl;
                cur = cur->next;
            }

            system("pause");
    }

    int main()
    {
        myStack q;
        char op;
        int key;

        for(int i = 0; i < 1; )
        {
            system("cls");
            cout<<"Press 1 to push value          :"<<endl;
            cout<<"Press 2 to pop value           :"<<endl;
            cout<<"Press 3 to Print Node View     :"<<endl;
            cout<<"Press 4 to Print Stack View    :"<<endl;
            cout<<"Press esc to exit              :"<<endl;

            op = _getch();
            key = op;

            if(key == 49)
            {
                q.push();
            }

            else if(key == 50)
            {
                q.pop();
            }

            else if(key == 51)
            {
                q.noutput();
                cout<<"\n\n";
            }

            else if(key == 52)
            {
                q.output();
                cout<<"\n\n";
            }

            else if(key == 27)
                i++;
            else
            {
                cout<<"\a Invalid Value Enter Again:"<<endl;
                system("pause");
            }   
        }
        system("pause");
        return 0;
    }
#包括
#包括
#包括
使用名称空间std;
结构节点
{
节点*prev;
int-val;
节点*下一步;
};
类myStack
{
节点*第一;
节点*cur;
节点*prev0;
公众:
myStack():first(NULL)、cur(NULL)、prev0(NULL){}
无效推送();
void pop();
void noutput();
无效输出();
~myStack(){}
};
void myStack::push()
{
cur=新节点;
cur->prev=NULL;
cur->next=NULL;
库特普雷夫;
删除cur;
prev0->next=NULL;
cur=prev0;
}
void myStack::noutput()
{
cur=第一;
系统(“cls”);

coutpop方法存在处理堆栈中最后一项的错误

void myStack :: pop()
{   
    if((first != null) && (first == prev0))
    {
        delete first;
        first = prev0 = cur = null;
    }
    else
    {
        prev0 = cur->prev;
        delete cur;
        prev0->next = NULL;
        cur = prev0;
    }
}

我想你必须更具体一些,而不是不工作有一个错误,它丢弃了最后一个
cur
程序运行时没有任何错误。push函数工作正常。但当我尝试使用pop函数时,程序停止工作。程序运行时没有任何错误——但你说它有错误吗?仅供参考,堆栈绝对不需要前后指针集。前指针集就是所有需要的这也会使你的代码更加简单。第二,不要将收集数据与容器操作混为一谈。读取主文件中的输入,然后通过
void push(int值)将其推入堆栈
member。关于保留了多么糟糕的幻数代码的注释;您在这里有足够的工作要做。
cur
并不总是当前节点。如果您在pop之前调用
myStack::output()
,它将是一个空指针。感谢@drescherjm的更正,我已经相应地更新了答案。