[c++]代码库
#ifndef linkstack_H
#define linkstack_H
template <class T>
struct Node
{
T data;
Node<T> *next;
};
template<class T>
class linkstack
{
public:
linkstack();
~linkstack(){};
void push(T x);
T pop();
T GetTop()
{
if (top != NULL)return top->data;
else return 0;
}
int empty()
{
if (top == NULL)return 1;
else return 0;
}
private:
Node<T> * top;
};
template<class T>
linkstack<T>::linkstack()
{
top = NULL;
}
template<class T>
void linkstack<T>::push(T x)
{
Node<T> *s = new Node < T >;
s->data = x;
s->next = top;
top = s;
}
template<class T>
T linkstack<T>::pop()
{
T x; Node<T> *p = NULL;
if (top == NULL) throw "underflow";
x = top->data; p = top;
top = top->next;
delete p;
return x;
}
#endif