/***************************** WZ ASUST 2016
*****************************/
#include <iostream>
using namespace std;
const int StackSize=10;
template <class T>
class SeqStack
{
public:
SeqStack( ) ; //构造函数,栈的初始化
~SeqStack( ); //析构函数
void Push(T x); //将元素x入栈
T Pop( ); //将栈顶元素弹出
T GetTop( ); //取栈顶元素(并不删除)
bool Empty( ); //判断栈是否为空
private:
T data[StackSize]; //存放栈元素的数组
int top; //栈顶指针,指示栈顶元素在数组中的下标
};
template <class T>
SeqStack<T>::SeqStack( )
{
top=-1;
}
template <class T>
SeqStack<T>::~SeqStack( )
{
}
template <class T>
void SeqStack<T>::Push(T x)
{
if (top== StackSize-1) throw "上溢";
top++;
data[top]=x;
}
template <class T>
T SeqStack<T>::Pop( )
{
T x;
if (top==-1) throw "下溢";
x=data[top--];
return x;
}
template <class T>
T SeqStack<T>::GetTop( )
{
if (top!=-1)
return data[top];
}
template <class T>
bool SeqStack<T>::Empty( )
{
if(top==-1) return 1;
else return 0;
}
void test1()
{
SeqStack<int> a; //创建模板类的实例
if (a.Empty( )){
cout<<"栈空,执行入栈操作:"<<endl;
cout<<"对15和10执行入栈操作:"<<endl;
try
{
a.Push(15);
a.Push(10);
}
catch(char* wrong)
{
cout<< wrong;
}
cout<<"栈顶元素为:"<<endl; //取栈顶元素
cout<<a.GetTop( )<<endl;
cout<<"执行出栈操作:"<<endl;
cout<<a.Pop( )<<endl; //执行出栈操作
cout<<"栈顶元素为:"<<endl;
cout<<a.GetTop( )<<endl;
}
else{
cout<<"栈不空"<<endl;
}
}
int main()
{
test1();
return 0;
}不带拷贝构造,,,,
原文:http://wzsts.blog.51cto.com/10251779/1762370