The stack of elements into the stack
Link Stacks:
1#include <iostream>2#include <cstdio>3 using namespacestd;4 structStack5 { 6 Chardata;7Stack *Next;8 };9stack* Initstack ()/*request memory for the stack head pointer*/Ten { OneStack *s=NewStack; As->next=NULL; - returns; - } the intIsEmpty (Stack *s) - { - if(s->next==NULL) - return 1; + return 0; - } + voidPushstack (stack* S,Charc) A { atStack *p=NewStack; -P->next=s->Next; -s->next=p; -P->data=C; - } - intPopstack (Stack *s,Char*e) in { - if(IsEmpty (s)) to return 0; + Else - { theStack *p=s->Next; *S->next=p->Next; $*e=p->data;Panax Notoginseng delete p; - return 1; the } + } A intMain () the { +Stack *s=Initstack (); - Charc,e; $ while(SCANF ("%c", &c) &&c!='\ n') $ Pushstack (s,c); - while(!IsEmpty (s)) - { thePopstack (s,&e); -cout<<e;Wuyi } the}
Sequential Stacks:
1#include <iostream>2#include <cstdio>3 using namespacestd;4 structStack5 {6 Chars[ -];7 intTop,bottom;8 };9 voidInitstack (stack*St)Ten { Onest->top=0; ASt->bottom=0; - } - intIsEmpty (Stack *St) the { - if(st->top==st->bottom) - return 1; - return 0; + } - voidPushstack (Stack* St,Charc) + { Ast->top++; atst->s[st->top]=C; - } - intPopstack (Stack* St,Char*e) - { - if(IsEmpty (ST)) - return 0; in*e=st->s[st->top]; -st->top--; to return 1; + } - intMain () the { * stack q; $Initstack (&q);Panax Notoginseng Charc,e; - while(SCANF ("%c", &c) &&c!='\ n') thePushstack (&q,c); + while(!isempty (&q)) A { thePopstack (&q,&e); +cout<<e; - } $ $}
Link stacks-Book exercises