顺序栈

顺序栈

代码

#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
*/
posted @ 2021-07-22 21:17  Treasure_lee  阅读(14)  评论(0编辑  收藏  举报