顺序栈
来源:互联网 发布:挂画淘宝详情页模版 编辑:程序博客网 时间:2024/04/28 20:56
定义一个顺序栈,实现简单的功能
#include<iostream.h>
//#include<fstream>
const int maxsize=100;
class seqstack
{
public:
int stack[maxsize];//定义栈数组
int top;//指向栈顶位置的指针
void inistack(seqstack &s);//初始化栈
void push(seqstack &s,int x);//入栈
void pop(seqstack &s);//删除栈顶元素
int gettop(seqstack s);//取栈顶元素
bool empty(seqstack s);//判断栈是否为空
};
void seqstack::inistack(seqstack &s)
{
s.top=0;
}
void seqstack::push(seqstack &s ,int x)
{
if(s.top==maxsize-1) cout<<"overflow"<<endl;
else
{
s.top++;
s.stack[s.top]=x;
}
}
void seqstack::pop(seqstack &s)
{
if(s.top==0)
cout<<"underflow/n";
else
s.top--;
}
int seqstack::gettop(seqstack s)
{
if(s.top==0)
{
cout<<"underflow/n";
return 0;
}
else return s.stack[s.top];
bool seqstack::empty(seqstack s) void main()
}
{
if(s.top==0)
return true;
else
return false;
}
{
seqstack s;
s.inistack(s);
s.push(s,36);
int x=88;
s.push(s,x);
cout<<s.gettop(s)<<endl;
s.pop(s);
cout<<s.gettop(s)<<endl;
bool z=s.empty(s);
cout<<z<<endl;
}