顺序栈
栈同样也是用来储存一对一线性存储结构
栈的“存”和“取”要求:
- 1.一端开放,一端封闭
- 2.先进后出
向栈中添加元素称为“进栈”(压栈或者入栈)
向栈中提取元素称为“入栈”(弹栈)
![空栈示意图](http://data.biancheng.net/uploads/allimg/181202/2-1Q202131603419.gif)
![模拟栈存储元素 1](http://data.biancheng.net/uploads/allimg/181202/2-1Q2021316322O.gif)
![模拟栈存储{1,2,3,4}](http://data.biancheng.net/uploads/allimg/181202/2-1Q202131649636.gif)
#include <iostream> #include <vector> using namespace std; int push(int *a, int top, int elem) { a[++top] = elem; return top; } int pop(int *a, int top) { if (top == 0) { cout << "空栈" << endl; return -1; } cout << a[top] << "出栈" << endl; --top; return top; } int main() { int vec[100] = { 0 }, top = -1; for(int i = 0; i < 10; ++i) top = push(vec, top, i); for (int i = 0; i < 10; ++i) top = pop(vec, top); system("PAUSE"); return 0; }