/*多栈共享操作*/
#include<stdio.h>
#define stacksize 50
#define TRUE 1
#define FALSE 0
typedef struct{
char elem[stacksize];
int top[2];
}doublesequencestack;
/*双端顺序栈的初始化*/
void initial(doublesequencestack * S)
{
S->top[0]=-1;//构造一个双端顺序空栈
S->top[1]=stacksize;
printf("initial double-sequence-stack success!\n");
fflush(stdout);
}
/*双端顺序栈进栈操作*/
int doublesequencepushstack(doublesequencestack *S,int i)
{
char c;
int flag=1;
if(S->top[0]==S->top[1])
{
printf("The stack is full !");
fflush(stdout);
return FALSE;
}
else
{
printf("Please enter the stack[%d] number you want to push elements !",i);
fflush(stdout);
switch(i)
{
case 1:
while(flag)
{
c=getchar();
if(c!='$')
{
S->top[0]++;
S->elem[S->top[0]]=c;
}
else
{
flag=0;
printf("Stack success !\n");
fflush(stdout);
}
}break;
case 2:
while(flag)
{
c=getchar();
if(c!='$')
{
S->top[1]--;
S->elem[S->top[1]]=c;
}
else
{
flag=0;
printf("Stack success !\n");
fflush(stdout);
}
}break;
}
}
return TRUE;
}
/*双端顺序栈弹出栈顶元素*/
int doublesequencepopstack(doublesequencestack *S,int i)
{
switch(i)
{
case 1:
if(S->top[0]==-1)
{
printf("stack empty!\n");
fflush(stdout);
}
else
{
printf("The value of the stack top element is %c\n",S->elem[S->top[0]]);
fflush(stdout);
S->top[0]--;
}break;
case 2:
if(S->top[1]==stacksize)
{
printf("stack empty!\n");
fflush(stdout);
}
else
{
printf("The value of the stack top element is %c\n",S->elem[S->top[1]]);
fflush(stdout);
S->top[1]++;
}break;
}
return TRUE;
}
/*双端顺序栈所有元素出栈*/
int popallelement(doublesequencestack *S,int i)
{
switch(i)
{
case 1:
if(S->top[0]==-1)
{
printf("\n stack empty!\n");
fflush(stdout);
return TRUE;
}
else
{
printf("%2c",S->elem[S->top[0]]);
fflush(stdout);
S->top[0]--;
popallelement(S,i);
}break;
case 2:
if(S->top[1]==stacksize)
{
printf("\n stack empty!\n");
fflush(stdout);
return TRUE;
}
else
{
printf("%2c",S->elem[S->top[1]]);
fflush(stdout);
S->top[1]++;
popallelement(S,i);
}break;
}
return FALSE;
}
int main(void)
{
doublesequencestack *S;
/*形参的星号是指指针,就是结构体类型的指针,函数形参是一个结构体指针,
* 实参也需要一个该结构体类型的指针,也就是要实参是一个地址传给形参
* 故在函数调用是使用的是&符号,就是区地址,将结构体变量的地址传过去
* 这样在函数里对于形参的修改就是对实参结构体变量的修改。*/
initial(&S);//初始化
printf("Please input the element to enter the stack[1] and end with $ \n");
fflush(stdout);
doublesequencepushstack(&S,1);//左端入栈
printf("Please input the element to enter the stack[2] and end with $ \n");
fflush(stdout);
doublesequencepushstack(&S,2);//右端入栈
doublesequencepopstack(&S,1);//左端栈顶元素出
doublesequencepopstack(&S,2);//右端栈顶元素出
printf("The out-of-stack[1] elements are as follows... \n");
fflush(stdout);
popallelement(&S,1);//左端元素全部出栈
printf("The out-of-stack[2] elements are as follows... \n");
fflush(stdout);
popallelement(&S,2);//右端元素全部出栈
return 0;
}
调试结果
initial double-sequence-stack success!
Please input the element to enter the stack[1] and end with $
Please enter the stack[1] number you want to push elements !123456789$
Stack success !
Please input the element to enter the stack[2] and end with $
Please enter the stack[2] number you want to push elements !qawsedrftgyh$
Stack success !
The value of the stack top element is 9
The value of the stack top element is h
The out-of-stack[1] elements are as follows...
8 7 6 5 4 3 2 1
stack empty!
The out-of-stack[2] elements are as follows...
y g t f r d e s w a q
stack empty!