<pre name= "code" class= "CPP" >[cpp] Simple dual-end stack application source file section: #include <stdio.h> #define MAXSTACKSIZE 100typedef int Datatype;int len,x; #include "SeqStack.h" int main () {seqstack mystack;int i,n,m; Stackinitiate (&mystack); for (i=0;i<10;i++) Stackpush (&mystack,i+1,1); Stackprint (mystack,1); Stackprint (mystack,2);p rintf ("Please enter the element you want to insert and the number of stacks to insert: \ n"); scanf ("%d%d", &n,&m); Stackpush (&mystack,n,m);p rintf ("The following sequence is obtained after insertion: \ n"); Stackprint (mystack,1); Stackprint (mystack,2); Stackpop (&MYSTACK,&X,M); Stacktop (mystack,&x,m);p rintf ("The following sequence is obtained after deletion: \ n"); Stackprint (mystack,1); Stackprint (mystack,2); return 0;} Header file section: typedef struct{datatype STACK[MAXSTACKSIZE]; The declaration of the stack defines an int top1;int top2;} Seqstack;void stackinitiate (Seqstack *s)//stack initialization operation {s->top1=0; The storage unit of the array is not discarded---the second method s->top2=maxstacksize-1;} int Stacknotempty (Seqstack s,datatype where)//empty {if (where==1) {if (s.top1<=0)//Empty return 0;return 1;} Else{if (S.top2>=maxstaCKSIZE-1) return 0;return 1; Non-conforming on empty}}int Stackpush (seqstack *s,datatype x,datatype where)//into the stack {if (S->TOP1+1==S->TOP2)// Full {printf ("The stack is full cannot be inserted!") \ n "); return 0;} else//Not full then insert operation {if (where==1) {s->stack[s->top1]=x; s->top1++;} else//Select the inserted stack number for insert Operation {s->stack[s->top2]=x; s->top2--;}} return 1;} int Stackpop (Seqstack *s,datatype *d,datatype where)//out stack {if (where==1) {if (s->top1<=0) {printf ("Stack 1th is empty , no data elements out of the stack! \ n "); return 0;} S->TOP1--;*D=S->STACK[S->TOP1];} Else{if (s->top2>=maxstacksize-1) {printf ("stack 2nd is empty, no data elements out of the stack! \ n "); return 0;} S->TOP2++;*D=S->STACK[S->TOP2];} return 1;} int Stacktop (Seqstack s,datatype *d,datatype where)//return stack top element {if (where==1) {if (len<=0) {printf ("Stack 1th empty no data element out of stack! \ n "); return 0;} *D=S.STACK[LEN-1];} Else{if (len>maxstacksize-1) {printf ("stack 2nd is empty without data elements out of the stack! \ n "); return 0;} *D=S.STACK[LEN+1];} return 1;} When using, forget to protectWhat error will be caused by the memory header file code void Stackprint (Seqstack s,datatype where)//print function {if (where==1) {len=s.top1;if (len<=0) {printf ("Stack 1th has been NULL, no element output. \ n "); return;} printf ("The elements in stack 1th are: \ n"), while (len>0) {stacktop (s,&x,1);p rintf ("%d", x); len--;} printf ("\ n"); return;} Else{len=s.top2;if (len>=maxstacksize-1) {printf ("stack 2nd is empty without data elements out of the stack! \ n "); return;} printf ("The elements in stack 2nd are: \ n"), while (len<maxstacksize-1) {stacktop (s,&x,2);p rintf ("%d", x); len++;} printf ("\ n"); return;} return;}
Bidirectional stacks grown from both ends-C language version