顺序栈

栈同样也是用来储存一对一线性存储结构

栈的“存”和“取”要求:

  • 1.一端开放,一端封闭
  • 2.先进后出

 

 

向栈中添加元素称为“进栈”(压栈或者入栈)

向栈中提取元素称为“入栈”(弹栈)

 

空栈示意图


模拟栈存储元素 1

模拟栈存储{1,2,3,4}

 

#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;
}

 

posted @ 2018-12-29 19:58  Hk_Mayfly  阅读(172)  评论(0编辑  收藏  举报