#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; }
链栈不对!!!因为忘了初始化呜呜