#ifndef LINKSTACK_H
#define LINKSTACK_H
#include <iostream>
using namespace std;
template<typename T>
class LinkStackNode
{
public:
T data;
LinkStackNode<T>* link;
LinkStackNode(T& value):data(value),link(NULL){}
};
template<typename T>
class LinkStack
{
LinkStackNode<T>* tos;
public:
LinkStack():tos(NULL){}
void Push(T& value);
T Pop();
T& GetTop();
bool IsEmpty();
void MakeEmpty();
};
template<typename T>
void LinkStack<T>::Push(T& value)
{
LinkStackNode<T>* add = new LinkStackNode<T>(value);
add->link = tos;
tos = add;
}
template<typename T>
T LinkStack<T>::Pop()
{
assert(tos!=NULL);
LinkStackNode<T>* old = tos;
tos = tos -> link;
T data = old->data;
delete old;
return data;
}
template<typename T>
T& LinkStack<T>::GetTop()
{
assert(tos!=NULL);
return tos->data;
}
template<typename T>
bool LinkStack<T>::IsEmpty()
{
return tos == NULL;
}
template<typename T>
void LinkStack<T>::MakeEmpty()
{
while(!this->IsEmpty())
{
this->Pop();
}
}
#endif
链栈
最新推荐文章于 2022-08-17 11:17:33 发布