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

#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; }

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

 

上一篇:栈、链表等初始化的思考


下一篇:2028284-71-9,m-PEG6-(CH2)8-phosphonic acid亲水性PEG连接剂