template <class T>
class LinkStack
{
public:
template <class T>
class LinkStackNode
{
public:
T data;
LinkStackNode * link;
LinkStackNode(T& value):link(NULL),data(value){}
};
LinkStack():top(NULL){}
void Push(T& value);
T& GetTop();
T& Pop();
bool IsEmpty();
void MakeEmpty();
private:
LinkStackNode<T> * top;
};
template<class T>
void LinkStack<T>::Push(T &value)
{
LinkStackNode<T> * add=new LinkStackNode<T>(value);
add->link=top;
top=add;
}
template <class T>
T& LinkStack<T>::GetTop()
{
if(top==NuLL)
{
cout<<"栈为空!"<<endl;
exit(0);
}
else
{
return top->data;
}
}
template <class T>
T& LinkStack<T>::Pop()
{
if(top==NULL)
{
cout<<"栈为空!"<<endl;
exit(0);
}
else
{
LinkStackNode<T> * old=top;
top=top->link;
T data=old->data;
delete old;
return data;
}
}
template <class T>
bool LinkStack<T>::IsEmpty()
{
return top==NULL;
}
template <class T>
void LinkStack<T>::MakeEmpty()
{
while(!this->IsEmpty())
{
this->Pop();
}
}
转载于:https://blog.51cto.com/lp4083331/203148