实验二 顺序栈

#include<iostream>
using namespace std;
const int StackSize=10;
class SeqStack
{
public:
SeqStack(){top=-1;}
~SeqStack(){}
void Push(int x);
int Pop();
int GetTop()
{if(top!=-1) return data[top];}
int Empty();
private:
int data[StackSize];
int top;
};


void SeqStack::Push(int x)
{
if(top==StackSize-1)throw"上溢";
  1. data[++top]=x;
}


int SeqStack::Pop()
{
int x;
if(top==-1)throw"下溢";
x=data[top--];
return x;
}


int SeqStack::Empty()
{
if(top==-1) return 1;
else return 0;
}


void main()
{
SeqStack S;
cout<<"********************"<<endl;
cout<<"      顺序栈"<<endl;
cout<<"********************"<<endl;
cout<<endl;
if(S.Empty())
cout<<"栈非空"<<endl;
else
cout<<"栈为空"<<endl;
cout<<endl;
cout<<"对15和10执行入栈操作"<<endl;
S.Push(15);
S.Push(10);
cout<<endl;
cout<<"栈顶元素为:"<<endl;
cout<<S.GetTop()<<endl;
cout<<endl;
cout<<"执行一次出栈操作"<<endl;
S.Pop();
cout<<endl;
cout<<"执行一次出栈操作后栈顶元素为:"<<endl;

cout<<S.GetTop()<<endl;}


猜你喜欢

转载自blog.csdn.net/laufen_j/article/details/80081835