Title Description:
/*
Title Description: The two stacks of numbers 0 and 1 are stored in an array space of space v[m], where the bottom of the stack is at both ends of the array.
When the stack top pointer of stack No. 0 top[0]=-1, the stack is empty, and when the stack top pointer of the 1th stack is top[1]=m, the stack is empty.
Each of the two stacks grows from both ends to the middle. Try to write a double stack initialization, judge the stack empty, stack full, into the stack, out of the stack and other functions of the algorithm.
The two-stack structure is defined as follows:
typedef struct{
int top[2],bot[2];//stack top and stack bottom pointer
Selemtype *v;//Stack Array
In m;//stack the maximum number of elements that can be accommodated (that is, the array length)
}dblstack;
*/
Define
typedef struct{
int top[2],bot[2];
int *v;
int m;
} Dblstack;
The implementation is as follows:
#include <stdio.h> #include <malloc.h>//define typedef struct{INT TOP[2],BOT[2];
int *v;
int m;
}dblstack; /* Initialize: Allocates an array space of size m for the stack space.
The initial value of the stack top pointer and the bottom pointer of stack No. 0 is-1; The initial value of the stack top pointer and the bottom pointer of stack 1th is M.
Represents a stack empty.
*/int Initstack (Dblstack &s,int m) {//s.v=new int[m];//is written in the following statement as well s.v= (int *) malloc (m*sizeof (int)); if (!
S.V) {Return 0;//storage allocation failed} S.top[0]=-1;
S.bot[0]=-1;
S.top[1]=m;
S.bot[1]=m;
return 1;
}/* To determine if the double stack is empty: Stack top of stack No. 0 and the initial value of the bottom pointer are-1; The initial value of the stack top pointer and the bottom pointer of stack 1th is M.
Represents a stack empty. */int IsEmpty (Dblstack &s) {if ((S.top[0]==s.bot[0]) && (s.top[1]==s.bot[1]) {return 1;//stack empty}else{ret
Urn 0;//stack non-empty}}/* Determine if the stack is full: When S.top[1]-s.top[0]==1, the stack is full. */int Isfull (Dblstack &s) {if (s.top[1]-s.top[0]==1) {return 1;//stack full}else{return 0;//stack Not satisfied}}/* Stack: First determine if the stack is full
Returns 0 if full, otherwise 1. */int Push (Dblstack &s,int e1,int E2) {//element E1 enters stack No. 0, element E2 enters 1th stack if (Isfull (S)) {return 0;//stack full, unable to enter stack}//*++s.top[
0]=e1;
*++s.top[1]=e2;
S.v[++s.top[0]]=e1;
Because the stack space is placed in the array, the in action is similar to adding elements to the array. s.v[--s.top[1]]=e2;
return 1;
}/* Out stack: First determine whether the stack is empty, if NULL returns 0; otherwise 1 is returned.
*/int Pop (dblstack &s,int &e1,int &e2) {//returns the top element of stack No. 0 with E1, and returns the top element of stack 1th with E2.
if (IsEmpty (S)) {return 0;
}//e1=*--s.top[0];
E2=*--S.TOP[1];
The e1=s.v[s.top[0]--];//stack operation is also similar to an array operation.
E2=s.v[s.top[1]++];
return 1;
} int main () {Dblstack S;
int m;
printf ("Please enter the array space size of the double stack:");
scanf ("%d", &m);
if (Initstack (s,m)) {printf ("Double stack initialization succeeded!\n");
}else{printf ("Double stack initialization failed!\n");
} if (IsEmpty (s)) {printf ("Double stack S is empty!\n");
}else{printf ("Double stack s non-empty!\n");
} if (Isfull (S)) {printf ("Double stack full!\n");
}else{printf ("Double stack not full!\n");
} int n;
printf ("Please enter the number of elements per stack in the stack:");
scanf ("%d", &n);
for (int i=0;i<n;i++) {int e1,e2;
printf ("Please enter%d elements per stack:", i+1);
scanf ("%d%d", &e1,&e2);
if (Push (s,e1,e2)) {printf ("Stack succeeded \ n");
}else{printf ("failed in stack \ n");
}} int e1,e2;
Pop (S,E1,E2);
printf ("Stack top element of two stacks:%d%d", e1,e2); }