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