#include <iostream>
using namespace std;
const int stacksize = 100;
//*****************************************************
声明栈的顺序表的类
class SeqStack
{
public:
SeqStack(){top=-1;}//构造函数,只需要将top值设定为-1
~SeqStack(){}//析构函数
void Push(int x);
int Pop();
int GetTop(){if(top!=-1) return data[top];}
void Clear(){top=-1;}
void Empty();
private:
int data[stacksize];
int top;
};
//push后 top加1 所以用++top;
void SeqStack::Push(int x)
{
if(top==stacksize-1)
cout<<"None"<<endl;
else
data[++top]=x;
}
//*****pop后top值减一,如果top为-1则证明栈空
int SeqStack::Pop()
{ int x;
if(top==-1)
cout<<"None"<<endl;
else{
x=data[top--];
return x;
}
}
//***********用来判断是否为空的函数
void SeqStack::Empty(){
if(top==-1)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
//主函数
int main()
{
SeqStack s; //声明s这个对象
char ch;
int x;
while(cin>>ch){
if(ch=='P'){
cin>>x;
s.Push(x);
}
else if(ch=='D'){
s.Pop();
}
else if(ch=='G'){
cout<<s.GetTop()<<endl;
}
else if(ch=='T'){
s.Clear();
}
else if(ch=='Y'){
s.Empty();
}
else if(ch=='E'){
break;
}
else
cout<<"错误"<<endl;
}
}
用C++完成的栈的顺序表的储存操作,今天传到csdn为了以后参考,希望大家指导一下,教教萌新一些写代码的基本习惯。