C Code Implementation stack

Source: Internet
Author: User

1# include <stdio.h>2# include <malloc.h>3# include <stdlib.h>4 //C language Implementation stack5 6 //Knot Point7typedefstructnode{8     intData//Data9     structnode* Pnext;//PointersTen}*Pnode,node; One  A //Stack -typedefstructstack{ - Pnode ptop; the Pnode Pbottom; -}*Pstack,stack; -  - voidInit (pstack s); + voidPush (Pstack S,intnum); - BOOLIsEmpty (Pstack s); + BOOLPop (pstack s); A voidTraverse (Pstack s); at voidClear (Pstack s); -  - intMainvoid) - { - STACK S; -Init (&s); in     if(IsEmpty (&s)) { -printf"stack is empty \ n"); to     }  +     //Press Stack -Push (&s,1); thePush (&s,4); *Push (&s,5); $Push (&s, the);Panax NotoginsengPush (&s, -); -Push (&s, One); thePush (&s,2); +     //Traverse ATraverse (&s); the     //out of the stack +     if(Pop (&s)) { -printf"the stack succeeds \ n"); $     } $     Else{ -printf"the stack failed \ n"); -     } theTraverse (&s); -Clear (&s);Wuyi         if(IsEmpty (&s)) { theprintf"stack is empty \ n"); -     }  Wu     return 0; - } About  $ //Initialize an empty stack - voidInit (pstack s) - { -S->ptop = (pnode)malloc(sizeof(NODE)); A     //determine if the application failed +     if(NULL = = s->ptop) { theprintf"Memory request failed"); -Exit (-1); $     } theS->pbottom = s->ptop; theS->pbottom->pnext =NULL; the } the  - //Press Stack in voidPush (Pstack S,intnum) the { the     //apply for a temporary junction. AboutPnode tem = (pnode)malloc(sizeof(NODE)); theTem->data =num; theTem->pnext = s->ptop; theS->ptop =tem; + } -  the //determine if the stack is emptyBayi BOOLIsEmpty (pstack s) the { the     returnS->ptop==s->Pbottom;  - } -  the //out of the stack the BOOLPop (pstack s) the { the     if(IsEmpty (s)) { -         return false; the     } the     Else{ thePnode tem = s->ptop;94S->ptop = tem->Pnext; the          Free(TEM); theTEM =NULL; the         return true;98     } About } - 101 //Traversal Stack102 voidTraverse (pstack s)103 {104Pnode p = s->ptop; the      while(p!=s->Pbottom) {106printf"%d",p->data);107p = p->Pnext;108     }109printf"\ n"); the }111  the //Empty Stack113 voidClear (pstack s) the { the     if(IsEmpty (s)) { the         return;117     }118     119Pnode p = s->ptop; -Pnode q =NULL;121      while(P! = s->Pbottom) {122Q = p->Pnext;123          Free(p);124p =Q; the     }126S->ptop = s->Pbottom;127}

C Code Implementation stack

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.