两栈共享空间

以下是两栈共享空间的代码实现,C语言

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define stduent student
#define max 5
struct stduent{
    int data[max];
    int top1;
    int top2;
};

int push(struct stduent *stack,int e,int stacktype){
    char a;
    if(stack->top1+1==stack->top2){
        printf("栈满,最后一个元素无法录入");
        return 0;
    }
    if(stacktype==1){
        stack->data[++stack->top1] = e;
    }else if(stacktype==2){
        stack->data[--stack->top2] = e;
    }
    printf("是否继续输入:(y/n)");
    scanf("\n");
    scanf("%c", &a);
    if(a=='y'){
        return 1;
    }else{
        return 0;
    }
    
}

void put(struct stduent *stack,int stacktype){
    int t1, t2;
    t1 = stack->top1, t2 = stack->top2;
    if(stacktype==1){
        if(stack->top1==-1){
            printf("栈空");
            return;
        }else{
            printf("以下为栈1元素:\n");
            while(stack->top1!=-1){
                printf("%d ", stack->data[stack->top1--]);
            }
            printf("\n遍历完毕");
        }
    }else if(stacktype==2){
        if(stack->top2==max){
            printf("栈空");
            return;
        }else{
            printf("以下为栈2元素:\n");
            while(stack->top2!=max){
                printf("%d ", stack->data[stack->top2++]);
            }
            printf("\n遍历完毕");
        }
    }else{
        if(stack->top1==-1&&stack->top2==max){
            printf("栈空");
            return;
        }else{
            printf("以下为全栈元素:\n");
            while(stack->top1!=-1){
                printf("%d ", stack->data[stack->top1--]);
            }
            while(stack->top2!=max){
                printf("%d ", stack->data[stack->top2++]);
            }
            printf("\n遍历完毕");
        }
    }
    stack->top1 = t1, stack->top2 = t2;//遍历完成后还原指针
}

void pop(struct student *stack,int *e,int stacktype){
    if(stacktype==1){
        if(stack->top1==-1){
            printf("栈空");
            return;
        }else{
            *e = stack->data[stack->top1--];
        }
    }else{
        if(stack->top2==max){
            printf("栈空");
            return;
        }else{
            *e = stack->data[stack->top2++];
        }
    }
    printf("取出的栈%d顶的元素为:%d", stacktype, *e);
}

int main(){
    struct student *stack = (struct student *)malloc(sizeof(struct student));
    int data, type, e;
    stack->top1 = -1;
    stack->top2 = max;
    do{
        printf("请输入元素 栈序号(1/2):\n");
        scanf("%d %d", &data, &type);
    }while (push(stack, data, type));
    printf("\n请输入要打印的栈(同时打印两栈请输入3): ");
    scanf("%d", &type);
    put(stack, type);
    printf("\n请输入要提取栈顶元素的栈: ");
    scanf("%d", &type);
    pop(stack, &e, type);
    printf("\n请输入要打印的栈(同时打印两栈请输入3): ");
    scanf("%d", &type);
    put(stack, type);
    return 0;
}

有帮助的话给个赞呗 ^_^

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值