其他分享
首页 > 其他分享> > 数据结构实验二-----入栈出栈基本操作(忘记初始化)

数据结构实验二-----入栈出栈基本操作(忘记初始化)

作者:互联网

#include<iostream>
#include<string>
#include<cstring>
using namespace std;
typedef struct StackNode *LinkStack;
struct StackNode {
    int data;
    LinkStack next;
};
int flag;
int e;
void InitStack(LinkStack &s)
{
    s = NULL;
}
void Push(LinkStack &s,int e)
{
    LinkStack p = new StackNode;
     p->data=e;
    p->next = s;
    s = p;
    
}
void Pop(LinkStack &s,int &e)
{
    if (s == NULL)
    {
        flag = 0;
        cout << "POP ERROR" << endl;
        return;
    }
    else
    {
        LinkStack p = s;
        if (s->next == NULL)
        {
            s = NULL;
        }
        else
        {
            StackNode *p = s;
            s = s->next;
            cout << p->data << endl;
            delete p;
        }
        
    }
}

int main()
{
    int n;
    while (cin >> n && n != 0)
    {
        LinkStack s;
InitStack(s);
for (int i = 0; i < n; i++) { flag = 1; int p; cin >> p; if (p!= -1) { Push(s, p); } else { Pop(s,e); if (flag == 0) { break; } } } } return 0; }

链栈不对!!!因为忘了初始化呜呜

 

标签:LinkStack,出栈,入栈,int,next,flag,StackNode,基本操作,NULL
来源: https://www.cnblogs.com/h694879357/p/11708253.html