//顺序栈
#include <iostream>
#define StackSzie 100
using namespace std;
typedef char DataType;
typedef struct{
DataType data[StackSzie];
int top;
}SeqStack;
//置空栈
void InitStack(SeqStack *s){
s->top=-1;
}
//判断空
int StackEmpty(SeqStack *s){
return s->top==-1;
}
//判断满
int StackFull(SeqStack *s){
return s->top==StackSize-1;
}
//进栈
void push(SeqStack *s,DataType x){
if(StackFull(s)){
cout<<"Stack overflow"<<endl;
}
s->data[++s->top]=x;
}
//退栈
DataType Pop(SeqStack *s){
if(StackEmpty(s)){
cout<<"Stack underflow"<<endl;
}
return s->data[s->top--];
}
//取栈顶元素
DataType StackTop(SeqStack *s){
if(StackEmpty(s)){
cout<<"Stack Empty"<<endl;
}
return s->data[s->top];
}
int main(){
SeqStack s;
push(&s,'r');
cout<<StackTop(&s)<<endl;
system("pause");
}
顺序栈
最新推荐文章于 2022-10-21 21:15:54 发布