双端栈

#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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值