# Include <iostream>
Using namespace STD;
Typedef char elemtype;
/* Stack basic operations with a header node, a true header pointer, and a tail pointer */
Struct Stack
{
Elemtype data;
Struct stack * next;
}; // * Pstack;
// Pstack pTop;
// Pstack pbottom; // defines the head pointer and tail pointer.
Stack * pTop, * pbottom;
/* Generate a header node. Both the tail pointer and the header pointer point to it */
Void initstack ()
{
Pbottom = new stack;
Pbottom-> next = NULL;
PTop = pbottom;
}
Void display ()
{
Stack * point = pTop;
While (point! = Pbottom)
{
Cout <point-> data <"" <Endl;
Point = point-> next;
}
Cout <Endl;
}
/* Determine whether the stack is empty */
Bool isempty ()
{
If (pbottom = pTop)
Return true;
Else
Return false;
}
/* Get the top element of the stack */
Void gettop ()
{
If (isempty () = true)
Cout <"Stack is empty" <Endl;
Else
Cout <"top stack element:" <pTop-> data <Endl;
}
/* Stack entry */
Void pushstack ()
{
Stack * point = new stack;
Cout <"enter a value" <Endl;
Cin> point-> data;
Point-> next = pTop;
PTop = point;
}
/* Output stack */
Void popstack ()
{
If (isempty () = true)
Cout <"Stack is empty" <Endl;
Else
{Cout <"the output stack element is:" <pTop-> data;
PTop = pTop-> next;
}
}
Int main ()
{
Initstack ();
Gettop ();
Pushstack ();
Pushstack ();
Pushstack ();
Display ();
Popstack ();
Display ();
Display ();
Pushstack ();
Gettop ();
Display ();
Return 0;
}
With regard to stack destruction, delelte can only recycle new nodes. Reference http://www.cnblogs.com/uniqueliu/category/307731.html