1#include <stdio.h>2#include <stdlib.h>3typedefCharDatetype;4typedefstructStacknode5 {6 Datetype date;7 structStacknode *Next;8 }stacknode;9typedefstruct Ten { OneStacknode *top;/*stack Top pointer*/ A }linkstack; - - voidInitstack (Linkstack *s)/*Initialize stack operation*/ the { -s->top==NULL; - } - + intStackempty (Linkstack *s)/*Judging the stack empty operation*/ - { + if(s->top==NULL) A return 1; at Else - return 0; - } - - intPush (Linkstack *s,datetype *x) - { inStacknode *p; -p= (Stacknode *)malloc(sizeof(Stacknode));/*Create a new node*/ to if(p==NULL) + return 0; - Else the { *p->date=x;/*put x in the data field of the new node*/ $p->next=s->top;/*Insert a new node before the list header*/Panax Notoginsengs->top=p;/*new node as the top of the stack.*/ - return 1; the } + } A the intPop (Linkstack *s) + { -Stacknode *p; $ Datetype x; $ if(Stackempty (s)) - { -printf"Stack Empty"); the return 0; - }Wuyi Else the { -P=s->top; WuX=p->date; -S->top=p->Next; About Free(p); $ returnx; - } - } - A intMain () + { the Linkstack S; - Datetype x; $Initstack (&s); theprintf"Stack element:\n"); the while((X=getchar ())! ='\ n') the { thePush (&s,&x); - } in while(!stackempty (&s)) the { theprintf"%c", Pop (&s)); About } the}
Application of Stacks