Bidirectional stacks grown from both ends-C language version

Source: Internet
Author: User

<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 (&AMP;MYSTACK,&AMP;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-&GT;TOP1+1==S-&GT;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-&GT;TOP1--;*D=S-&GT;STACK[S-&GT;TOP1];} Else{if (s->top2>=maxstacksize-1) {printf ("stack 2nd is empty, no data elements out of the stack! \ n "); return 0;} S-&GT;TOP2++;*D=S-&GT;STACK[S-&GT;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.