这里写一个最简单的栈:
主类:
class seStack
{
int data[100];//数据
int top;//栈顶
public:
seStack();
~seStack();
void Pushin(int x);//入栈
int Pop();//出栈
int gettop();//获得栈顶数据
bool emptys();//检查是否为空
};
各种实现函数
检查:
bool seStack::emptys()
{
if(top==-1)
{
return true;
}
else
{
return false;
}
}
返回顶数据
int seStack::gettop()
{
if(emptys())
{
cout<<"空";
}
else
{
return data[top];
}
}
出栈
int seStack::Pop()
{
if(emptys())
{
cout<<"空";
}
else
{
int x=data[top];
top--;
return x;
}
}
入栈
void seStack::Pushin(int x)
{
if(top==maxsize-1)
{
cout<<"越界";
}
else
{
top++;
data[top]=x;
}
}
构造函数
seStack::seStack()
{
top=-1;
}
这个栈很简单。