5-顺序栈的操作

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef int ElemType;
/**/
#define  MaxSize 50
/**/
typedef struct {
    ElemType  data[MaxSize];
    /* top:数组索引从0开始 = MaxSize-1 */
    int top;
} SqStack;

/*初始化栈*/
void InitStack(SqStack *S){
    S->top=-1;
}

/*判断顺序栈空*/
bool StackEmpty(SqStack S){
    /*栈空*/
    if(S.top ==-1){
        return  true;
    }
    /*栈非空*/
    else{
        return  false;
    }
}
/*进栈操作*/
bool Push(SqStack *S,ElemType x){
    /*栈满*/
    if(S->top ==MaxSize-1){
        return  false;
    }
    /*先+1m,再送元素入栈*/
    S->data[++S->top]=x;
    return  true;
}
/* 出栈操作 */
bool Pop(SqStack *S, ElemType *x) {
    /* 栈空 */
    if (S->top == -1) {
        return false;
    }
    /* 先取栈顶元素,然后栈顶指针减1 */
    *x = S->data[S->top--];
    return true;
}
/*读取栈顶元素*/
bool GetTop(SqStack S,ElemType *x){
    /*栈空*/
    if(S.top ==-1){
        return  false;
    }
    /*读取栈顶元素*/
    *x=S.data[S.top];
    return true;
}
int main() {


    return 0;
}

posted @ 2024-07-10 17:23  成强  阅读(2)  评论(0编辑  收藏  举报