“300 人吃掉 5000 萬零食”,燒光 84 億的拜騰瀕臨破產
阿新 • • 發佈:2021-07-20
順序棧
順序棧
程式碼
#include <iostream> #include <cstring> #include <algorithm> using namespace std; #define MaxSize 10 typedef struct{ int data[MaxSize]; int top; } SqStack; //初始化棧 //也有S.top = 0 void InitStack(SqStack &S){ S.top = -1; } bool StackEmpty(SqStack &S){ if (S.top == -1) return true; else return false; } bool Push(SqStack &S,int x){ if (S.top == MaxSize - 1) return false; S.data[++S.top] = x; return true; } bool Pop(SqStack &S,int &x){ if (S.top == -1) return false; x = S.data[S.top--]; return true; } bool GetTop(SqStack &S,int &x){ if (S.top == -1) return false; x = S.data[S.top]; return true; } int main() { SqStack S; InitStack(S); int n; cin >> n; for (int i = 0; i < n; i ++ ) { int x; cin >> x; Push(S,x); } cout <<"S.top: "<<S.top << endl; for (int i = 0; i < n; i ++ ) { int y; Pop(S,y); cout << y << ' '; } cout << endl << "S.top: "<<S.top << endl; return 0; } /* 4 1 0 2 4 */