Stack-out stack traversal stack instance code and instance code

Source: Internet
Author: User

Stack-out stack traversal stack instance code and instance code

# Include <stdio. h> # include <stdlib. h> # include <malloc. h> typedef struct Node // defines a linked list struct {int data; struct Node * pNext;} NODE, * PNODE; typedef struct Stack // defines a Stack struct {PNODE pTop; PNODE pBottom;} STACK, * PSTACK; void initStack (PSTACK); void pushStack (PSTACK, int); void travelStack (PSTACK); bool popStack (PSTACK, int *); void clear (PSTACK ps); int main () {int val; stack s; // defines an S variable, which has two parameters. One pTop, one pBottoninitStack (& S); // initialize the stack pushStack (& S, 1); // press the stack pushStack (& S, 2); pushStack (& S, 3); pushStack (& S, 4); pushStack (& S, 5); travelStack (& S); // traverses the output if (popStack (& S, & val )) // determine whether the stack is successfully output {printf ("the stack is successful, and the element of the stack is % d \ n", val );} else {printf ("failed to output stack \ n");} printf ("traverse output result \ n"); travelStack (& S ); // print the output clear (& S); printf ("print the following blank again, indicating that the result is cleared successfully! \ N "); travelStack (& S); // print the output return 0;} void initStack (PSTACK ps) // initialize the stack so that the two elements in the stack point to one node. {Ps-> pTop = (PNODE) malloc (sizeof (NODE); if (NULL = ps-> pTop) {printf ("dynamic memory allocation failed \ n "); exit (-1);} else {ps-> pBottom = ps-> pTop; ps-> pTop-> pNext = NULL; // equivalent: ps-> pBottom-> pNext = NULL; that is, the next inserted space is empty. That is, the top element of the stack is empty.} void pushStack (PSTACK ps, int val) // The pressure stack function {PNODE pNew = (PNODE) malloc (sizeof (NODE )); pNew-> data = val; pNew-> pNext = ps-> pTop; // each time the elements are pressed to the top of the stack ps-> pTop = pNew;} void travelStack (PSTACK ps) // traverse the stack {PNODE p = ps-> pTop; while (p! = Ps-> pBottom) {printf ("% d", p-> data); p = p-> pNext;} printf ("\ n"); return ;} bool empty (PSTACK ps) // judge whether the stack is empty {if (ps-> pTop = ps-> pBottom) return true; elsereturn false ;} // place the ps to the stack once, and store the elements of the stack into the variable pointed to by the pVal parameter. // If the stack fails, false is returned. Otherwise, returns truebool popStack (PSTACK ps, int * pVal) {if (empty (ps) {return false;} else {PNODE r = ps-> pTop; * pVal = r-> data; ps-> pTop = r-> pNext; free (r); r = NULL; return true ;}// finally clear the stack. Void clear (PSTACK ps) {if (empty (ps) {return;} else {PNODE p = ps-> pTop; PNODE q = NULL; while (p! = Ps-> pBottom) {q = p-> pNext; free (p); p = q;} ps-> pTop = ps-> pBottom ;}}


 

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.