一本全面剖析C++数据结构算法的书籍
源代码在线查看: lstack.h
// file lstack.h // linked stack #ifndef LinkedStack_ #define LinkedStack_ #include "node.h" #include "xcept.h" template class LinkedStack { public: LinkedStack() {top = 0;} ~LinkedStack(); bool IsEmpty() const {return top == 0;} bool IsFull() const; T Top() const; LinkedStack& Add(const T& x); LinkedStack& Delete(T& x); private: Node *top; // pointer to top node }; template LinkedStack::~LinkedStack() {// Stack destructor.. Node *next; while (top) { next = top->link; delete top; top = next; } } template bool LinkedStack::IsFull() const {// Is the stack full? try {Node *p = new Node; delete p; return false;} catch (NoMem) {return true;} } template T LinkedStack::Top() const {// Return top element. if (IsEmpty()) throw OutOfBounds(); return top->data; } template LinkedStack& LinkedStack::Add(const T& x) {// Add x to stack. Node *p = new Node; p->data = x; p->link = top; top = p; return *this; } template LinkedStack& LinkedStack::Delete(T& x) {// Delete top element and put it in x. if (IsEmpty()) throw OutOfBounds(); x = top->data; Node *p = top; top = top->link; delete p; return *this; } #endif;