Stack basic operations with header nodes, true header pointers, and tail pointers

Source: Internet
Author: User

# 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.