首页 > 其他 > 详细

顺序栈

时间:2021-07-22 23:35:56      阅读:35      评论:0      收藏:0      [点我收藏+]

顺序栈

代码

#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
*/

顺序栈

原文:https://www.cnblogs.com/lijiaji/p/15046468.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!