#include<stdio.h>
#include<stdlib.h>
#define N 5
typedef struct
{
int elem[N];
int top[2];
}DqStack;
int initStack(DqStack *L); //初始化
int pushStack(DqStack *L);//进栈
int popStack(DqStack *L);//出栈
int main(void)
{
DqStack L;//定义一个这样的节点
int b;
int a;
while (1)
{
printf("\n");
printf("*********************\n");
printf("1.Init DqStack\n");
printf("2.Push DqStack\n");
printf("3.Pop DqStack\n");
printf("4.EXIT\n");
printf("*********************\n");
printf("\n");
scanf("%d", &a);
switch (a)
{
case 1:b = initStack(&L);
if (b == 1)
{
printf("Init Success!\n");
}
break;
case 2:b = pushStack(&L);
if (b == 0)
{
printf("Push Fail\n");
}
if (b == 1)
{
printf("Push Success\n");
}
if (b == 2)
{
printf("You input the number of stack is error\n");
}
break;
case 3:b = popStack(&L);
if (b == 0)
{
printf("Pop Fail\n");
}
if (b == 1)
{
printf("Pop Success\n");
}
break;
if (b == 2)
{
printf("You input the number of stack is error\n");
}
break;
case 4:exit(0);
default:printf("You input the number of the menu is error\n");
break;
}
}
return 0;
}
int initStack(DqStack *L)
{
L->top[0] = -1;
L->top[1] = N;
return 1;
}
int pushStack(DqStack *L)
{
int i;
int a;
int flag = 1;
if (L->top[0] + 1 == L->top[1])//栈满
{
return 0;
}
printf("Please input the number of the stack:\n");
scanf("%d", &i);
switch (i)
{
case 0:
printf("Please input the number that you want to insert (end by 0):\n");
while (flag)
{
scanf("%d", &a);
if (a == 0)
{
flag = 0;
continue;
}
if (L->top[0] + 2 == L->top[1])//栈满
{
L->elem[++(L->top[0])] = a;
printf("You should input 0 to end input\n");
}
else
{
L->elem[++(L->top[0])] = a;
}
}
break;
case 1:
printf("Please input the number that you want to insert (end by 0):\n");
while (flag)
{
scanf("%d", &a);
if (a == 0)
{
flag = 0;
continue;
}
if (L->top[0] + 2 == L->top[1])//栈满
{
L->elem[--(L->top[1])] = a;
printf("You should input 0 to end input\n");
}
else
{
L->elem[--(L->top[1])] = a;
}
}
break;
default:
return 2;
}
return 1;
}
int popStack(DqStack *L)
{
int i;
int a;
printf("Please input the number of the stack that you want pop\n");
scanf("%d", &i);
switch (i)
{
case 0:
printf("The Stack is:\n");
while (L->top[0] != -1)
{
a = L->elem[L->top[0]--];
printf("%d\t", a);
}
printf("\n");
break;
case 1: printf("The Stack is:\n");
while (L->top[1] != N)
{
a = L->elem[L->top[1]++];
printf("%d\t", a);
}
printf("\n");
break;
default:return 2;
break;
}
return 1;
}
双端栈
最新推荐文章于 2022-07-13 17:53:32 发布