#include <iostream>
using namespace std;
template<class DataType>
struct Node
{
DataType data;
Node<DataType>*next;
};
template<class DataType>
class LinkStack
{
public:
LinkStack(){top=new Node<DataType>; top=NULL;};
~LinkStack(){};
void Push(DataType x);
DataType Pop();
DataType GetTop();
int Empty();
private:
Node<DataType>*top;
};
template<class DataType>
void LinkStack<DataType>::Push(DataType x)
{
Node<DataType>*s;
s=new Node<DataType>;
s->data=x;
s->next=top;
top=s;
}
template<class DataType>
DataType LinkStack<DataType>::Pop()
{
DataType x;
if(top==NULL)throw"下溢";
else
{
Node<DataType>*p;
p=new Node<DataType>;
p=top;
x=p->data;
top=top->next;
delete p;
return x;
}
}
template<class DataType>
DataType LinkStack<DataType>::GetTop()
{
if (top!=NULL);
return top->data;
}
template<class DataType>
int LinkStack<DataType>::Empty()
{
if(top==NULL)return 1;
else return 0;
}
void main()
{
LinkStack<int>S;
if(S.Empty())
cout<<"栈为空"<<endl;
else
cout<<"栈非空" <<endl;
cout<<"对9和21执行入栈操作"<<endl;
S.Push(9);
S.Push(21);
cout<<"栈顶元素为:";
cout<<S.GetTop()<<endl;
cout<<"执行一次出栈操作"<<endl;
S.Pop();
cout<<"栈顶元素为:";
cout<<S.GetTop()<<endl;
using namespace std;
template<class DataType>
struct Node
{
DataType data;
Node<DataType>*next;
};
template<class DataType>
class LinkStack
{
public:
LinkStack(){top=new Node<DataType>; top=NULL;};
~LinkStack(){};
void Push(DataType x);
DataType Pop();
DataType GetTop();
int Empty();
private:
Node<DataType>*top;
};
template<class DataType>
void LinkStack<DataType>::Push(DataType x)
{
Node<DataType>*s;
s=new Node<DataType>;
s->data=x;
s->next=top;
top=s;
}
template<class DataType>
DataType LinkStack<DataType>::Pop()
{
DataType x;
if(top==NULL)throw"下溢";
else
{
Node<DataType>*p;
p=new Node<DataType>;
p=top;
x=p->data;
top=top->next;
delete p;
return x;
}
}
template<class DataType>
DataType LinkStack<DataType>::GetTop()
{
if (top!=NULL);
return top->data;
}
template<class DataType>
int LinkStack<DataType>::Empty()
{
if(top==NULL)return 1;
else return 0;
}
void main()
{
LinkStack<int>S;
if(S.Empty())
cout<<"栈为空"<<endl;
else
cout<<"栈非空" <<endl;
cout<<"对9和21执行入栈操作"<<endl;
S.Push(9);
S.Push(21);
cout<<"栈顶元素为:";
cout<<S.GetTop()<<endl;
cout<<"执行一次出栈操作"<<endl;
S.Pop();
cout<<"栈顶元素为:";
cout<<S.GetTop()<<endl;
}