#ifndef Linkstack_H
#define Linkstack_H
#include
using namespace std;
template
struct Node
{
T data;
Nodenext;
};
template
class LinkStack
{
public:
LinkStack();//{top=NULL;}
~LinkStack();
void Push(T x);
T Pop();
T GetTop();
int Empty();//{top=NULL?return 1:return 0;}
private:
Node top;
};
#endif
#include
using namespace std;
#include"Link.h"
template
LinkStack::LinkStack()
{
top=new Node;
top=NULL;
}
template
void LinkStack::Push(T x)
{
Node*s;
s=new Node;
s->data=x;
s->next=top;
top=s;
}
template
T LinkStack::Pop()
{
Node*p;
p=new Node;
if(top==NULL)throw"下溢";
int x=top->data;
p=top;
delete p;
return x;
}
template
LinkStack::~LinkStack()
{
while(top)
{
Node*p;
p=top->next;
delete top;
top=p;
}
}
template
T LinkStack::GetTop()
{
if(top!=NULL)
return top->data;
else
return 0;
}
int main()
{
LinkStack x;
try
{
x.Push(1);
x.Push(2);
x.Push(8);
cout<<x.GetTop()<<endl;
x.Pop();
cout<<x.GetTop()<<endl;
x.Pop();
x.Pop();
x.Pop();
x.Empty();
cout<<x.GetTop()<<endl;
}
catch(char*wrong)
{
cout<<wrong;
}
}