数据结构——栈及相关操作

#include<bits/stdc++.h>

#define MaxSize 10
#define ElementType int

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

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

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

bool Push(SqStack &S,ElementType x){
    if(S.top == MaxSize - 1){
        printf("Stack is full!");
        return false;
    }
    S.top++;
    S.data[S.top] = x;
    return true;
}

bool Pop(SqStack &S,ElementType &x){
    if(StackEmpty(S)){
        return false;
    }
    x = S.data[S.top];
    S.top--;
    return true;
}

bool Get_TopElem(SqStack &S,ElementType &x){
    if(StackEmpty(S)){
        return false;
    }
    x =S.data[S.top];
    return true;
}

bool print_Stack(SqStack &S){
    if(StackEmpty(S)){
        return false;
    }else{
        for(int i=S.top;i>=0;i--){
            printf("%d\n",S.data[i]);
        }
        return true;
    }
}

void test(){
    SqStack S;
    InitStack(S);
    Push(S,1);
    Push(S,2);
    Push(S,3);
    print_Stack(S);
}

int main()
{
    test();
    return 0;
}
posted @ 2024-01-21 14:56  想成为编程高手的阿曼  阅读(3)  评论(0编辑  收藏  举报