template
class LinkedStack;
template
class Node
{
friend class LinkedStack;
private:
T data;
Node *link;
};
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;
};
template
LinkedStack::~LinkedStack()
{
Node* next;
while(top)
{
next=top->link;
delete top;
top=next;
}
}
template
bool LinkedStack::IsFull()const
{
try
{
Node* p=new Node;
delete p;
return false;
}
//catch(NoMem){return true;}
catch(){}
}
template
T LinkedStack::Top() const
{
//if(IsEmpty())throw OutOfBounds();
if(IsEmpty())exit(0);
else
return top->data;
}
template
LinkedStack& LinkedStack::Add(const T &x)
{
Node* p=new Node;
p->data=x;
p->link=top;
top=p;
return *this;
}
template
LinkedStack& LinkedStack::Delete(T &x)
{
if(IsEmpty())throw OutOfBounds();
x=top->data;
Node* p=top;
top=top->link;
delete p;
return *this;
}