# ifndef LinkStack_H
# define LinkStack_H
template <class DataType>
class LinkStack
{
public:
LinkStack();
~ LinkStack(){};
void Push (DataType x);
DataType Pop();
DataType GetTop() ;
int Empty() ;
private:
Node <DataType>*top;
};
# endif
# include "LinkStack.h"
template <class DataType>
LinkStack<DataType>::LinkStack()
{
top=-1;
}
template <class DataType>
void LinkStack<DataType>::Push(DataType x)
{
s=new Node ;s->data=x;
s->next=top;top=s;
}
template<class DataType>
DataType LinkStack<DataType>::Pop()
{
if(top==-1)throw"下溢";
x= top->data;p=top;
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;
}
# include<iostream>
using namespace std;
# include "LinkStack.cpp"
void main()
{
LinkStack<int>S;
if(S.Empty())
cout<<"栈为空"<<endl;
else
cout<<"栈非空"<<endl;
cout<<"对5和3执行入栈操作"<<endl;
S.Push( 5);
S.Push(3);
cout<<"栈顶元素为:"<<endl;
cout<<S.GetTop()<<endl;
cout<<"执行一次出栈的操作"<<endl;
S.Pop();
cout<<"栈顶元素为:"<<endl;
cout<<S.GetTop()<<endl;
}
链栈
最新推荐文章于 2022-08-17 11:17:33 发布