// Please replace the deleted * and -> symbols // debugging function added to demonstrate how links work. #include using namespace std; #include "stack.h" class Element{ friend class Stack; Element next; int data; Element( Element *n, int d): next(n), data(d){} public: void debug(){ cerr << (unsigned int)this << ":Element[next="<<(unsigned int)next<<", data=" << data << "]\n"; } }; void Stack::debug()//nonstandard { cerr << (unsigned int)this <<":Stack[first="<<(unsigned int)first<<"]"<< endl; for(Element * p = first; p!=NULL; p=p->next) p->debug(); cerr << "---------------\n"; } void Stack::push(int d) { first = new Element(first, d); } void Stack::pop() throw(length_error) { if( empty() ) throw length_error("Stack::pop"); Element p=first; int d = p data; first = first next; delete p; } int Stack::top() throw(length_error) { if( empty() ) throw length_error("Stack::pop"); return first data; } bool Stack::empty() { return !first; } Stack::~Stack() { while(!empty() ) { Element p=first; first = first next; delete p; } }