#include<iostream> using namespace std; #define MaxSize 10 // 1. 顺序栈 typedef int ElemType; struct Stack {ElemType data[MaxSize];int top; } SqStack; // 初始化栈 void init(Stack &s) {// 初始化栈顶指针s.top = -1; } // 入栈 bool push(Stack &s, ElemType x) {if (s.top == MaxSize - 1) {return false;} s.top = s.top + 1;s.data[s.top] = x;return true; } // 出栈 bool pop(Stack &s, ElemType &x) {if(s.top == -1) {return false;}x = s.data[s.top];s.top = s.top - 1;return true; } // 遍历 void traverse(Stack s) {for (int i = 0; i <= s.top; i++) {cout << s.data[i] << " ";}cout << endl; } // 栈的长度 int length(Stack s) {return s.top + 1; } int main() {Stack s;init(s);ElemType x;push(s, 1);push(s, 2);push(s, 3);push(s, 4);push(s, 5);traverse(s);pop(s, x);cout << "pop: " << x << endl;traverse(s);cout << "length: " << length(s) << endl;return 0; }