代码实现
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define MaxSize 100
#define ElemType int
typedef struct{
ElemType data[MaxSize];
int top;
}SeqStack;
void InitStack(SeqStack *stack){
stack->top = -1;
};
bool StackEmpty(SeqStack stack){
if(stack.top == -1)
return true;
return false;
}
bool Push(SeqStack *stack,ElemType e){
if(stack->top == MaxSize){
return false;
}
stack->top++;
stack->data[stack->top] = e;
}
bool Pop(SeqStack *stack,ElemType *e){
if(StackEmpty(*stack)){
return false;
}
*e = stack->data[stack->top];
stack->top--;
return true;
}
bool GetTop(SeqStack stack,ElemType *e){
if(StackEmpty(stack)){
return false;
}
*e = stack.data[stack.top];
return true;
}
void DestroyStack(SeqStack *stack){
stack->top = -1;
}
int main(){
ElemType Elem;
SeqStack stack;
InitStack(&stack);
Push(&stack,1);
GetTop(stack,&Elem);
printf("当前栈顶元素为:%d\n",Elem);
Pop(&stack,&Elem);
printf("刚才出栈的元素为:%d\n",Elem);
DestroyStack(&stack);
return 0;
}