template <typename Object>
class LinkedStack {
protected: // local node structure
// ... (insert Node here)
private: // member data
NodePtr tp; // pointer to stack top
int sz; // number of items in stack
public:
LinkedStack() { // default constructor
tp = NULL;
sz = 0;
}
int size() const { return sz; } // number of elements in stack
bool isEmpty() const { return sz == 0; } // is the stack empty?
// return stack top
Object& top() throw(StackEmptyException) {
if (isEmpty())
throw StackEmptyException("Top of empty stack");
return tp->element;
}
void push(const Object& e) { // push element onto stack
NodePtr v = new Node(e, tp);
tp = v; // v is now the top
sz++;
}
Object pop() throw(StackEmptyException) { // pop top element
if (isEmpty())
throw StackEmptyException("Pop of empty stack");
NodePtr old = tp; // node to remove
tp = tp->next;
sz--;
Object result = old->element; // element to return
delete old;
return result;
}
// ... (insert housekeeping functions here)
};