栈
栈 后进先出,较为简单,不多言。
#include <iostream>
using namespace std;
#define MaxSize 20
typedef int elemtype;
typedef struct
{
elemtype data[MaxSize];
int top;
}SeqStack;
bool StackEmpty(SeqStack &s)
{
if (s.top==-1){
cout<<"Empty!!"<<endl;
return true;
}
else{
cout<<"Not empty"<<endl;
return false;
}
}
bool Push(SeqStack &s,int x)
{
if(s.top==MaxSize-1)
return false;
s.data[++s.top]=x;
return true;
}
bool Pop(SeqStack &s)
{
if (s.top==-1)
return false;
int x=s.data[s.top--];
cout<<"The result of Pop is : "<<x<<endl;
}
int GetTop(SeqStack &s)
{
if (s.top==-1)
cout<<"fail"<<endl;
return false;
cout<<"The top of stack is : "<<s.data[s.top]<<endl;
}
int main()
{
SeqStack s;
s.top=-1; //初始化
Push(s,2); //入栈
StackEmpty(s); //判空
GetTop(s); //查看栈顶元素
Pop(s); //获取栈顶元素
GetTop(s); //查看栈顶元素
StackEmpty(s); //判空
}