其他分享
首页 > 其他分享> > 顺序栈的出入栈

顺序栈的出入栈

作者:互联网

#include <stdio.h>

#define MaxSize 10

typedef struct{
    int  data[MaxSize];
    int  top;
}SqStack;

void InitSqStack(SqStack &S){
    S.top=-1;
}

bool Full(SqStack &S){
    if(S.top == MaxSize-1)
        return false;
    return true;
}

bool Empty(SqStack &S){
    if(S.top==-1)
        return false;
    return true;
}

bool push(SqStack &S,int e){
    if(Full(S)){
        S.data[++S.top]=e;
        return true;
    }
    return false;
}

bool pop(SqStack &S,int &x){
    if(Empty(S)){
        x=S.data[S.top--];
        return true;
    }
    return false;
}

int main() {
    SqStack S;
    int x;
    InitSqStack(S);
    push(S,1);
    push(S,2);
    push(S,3);
    pop(S,x);
    printf_s("%d",x);
    pop(S,x);
    printf_s("%d",x);
    pop(S,x);
    printf_s("%d",x);
    if(pop(S,x) == false)
        printf_s("出错啦");
}

标签:顺序,return,SqStack,int,出入,top,pop,false
来源: https://blog.csdn.net/FuckerGod/article/details/118642766