#include<iostream> #include<string> #include<cstring> #define maxsize 1000 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; s = s->next; cout << p->data << endl; delete p; } } int main() { int n; while (cin >> n && n!=0) { if (n < 0) continue; LinkStack s; InitStack(s); int a[maxsize]; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) { flag = 1; if (a[i]!= -1) { Push(s, a[i]); } else { Pop(s, e); if (flag == 0) { break; } } } } return 0; }
我认为这个题有问题。。
全部输入以后才输出!!!
没有读懂题意。
数据结构实验二-----入栈出栈基本操作(忘记初始化,而且没有读懂题意)
原文:https://www.cnblogs.com/h694879357/p/11708253.html