# include<stdio.h># include<malloc.h># include<stdlib.h>typedefstructnode{intdata; structNode *Pnext;} NODE,*Pnode;typedefstructstack{Pnode ptop; Pnode Pbottm;} STACK,*Pstack;voidInitstack (Pstack pS);voidPushstack (Pstack PS,intval);voidTraversestack (Pstack pS);intMainvoid) {STACK s; Initstack (&s); Pushstack (&s,1); Pushstack (&s,2); Pushstack (&s,3); Pushstack (&s,4); Pushstack (&s,5); Traversestack (&s); return 0;}voidInitstack (pstack PS) {PS->ptop= (Pnode)malloc(sizeof(NODE)); if(ps->ptop==NULL) {printf ("dynamic memory allocation failed! \ n"); Exit (-1); } Else{PS->pbottm=ps->ptop;//ps->pbottm->pnext=ps->ptop->pnext?ps->ptop->pnext=NULL; }}voidPushstack (Pstack PS,intval) {Pnode pnew= (Pnode)malloc(sizeof(NODE)); Pnew->data=Val; Pnew->pnext=ps->ptop;//because the first ptop and Pbottom point are the same node, so Ps->ptop=ps->pbottomps->ptop=pnew; return;}voidTraversestack (Pstack pS) {Pnode P=ps->Ptop; while(P!=ps->Pbottm) {printf ("%d",p->data); P=p->Pnext; } printf ("\ n"); return;}
Data structure (implementation and traversal of stacks) C language implementation