#include
using namespace std;
template
struct stacknode
{
private:
T data;
stacknode* link;
public:
stacknode(T d = 0, stacknode* next = NULL) :data(d), link(next) {};
};
template
class linkstack //
{
private:
stacknode* top; //栈顶指针
void ouput(ostream& s, stacknode* ptr, int i);
public:
linkstack() :top(NULL) {};
~linkstack() {};
void Push(T x);
bool Pop(T& x);
bool Isempty()const { return top == NULL; }
bool getTop(T& x)const;
};
template
linkstack::~linkstack() //top是头结点,链栈不带头结点。
{
stacknode* p;
while (top != NULL)
{
p = top->link;
delete top;
top = top->link;
}
}
template
void linkstack::Push(T x)
{
//top = new stacknode(x, top); //按照构造函数来
//以下用创建链表的方式创建(头插法)
stacknode* p=top;
top = new stacknode;
top->data = x;
top->link = p;
}
template
bool linkstack::Pop(T &x) //删除头结点的操作
{
if (Isempty() == true)return false;
stacknode* p = top;
top = top->link;
x = p->data;
delete p;
return true;
}
template
bool linkstack::getTop(T& x)const //删除头结点的操作
{
if (Isempty() == true)return false;
stacknode* p = top;
top = top->link;
x = p->data;
return true;
}