基础实验3-2.5-堆栈模拟队列-函数题

基础实验3-2.5-堆栈模拟队列-函数题

解题代码

#include<stdio.h>
#include<stdlib.h>
typedef struct stack* Stack;
struct stack {
	int *data;
	int top;
	int MaxSize;
};
int IsFull(Stack S) {
	return S->top == S->MaxSize - 1 ? 1 : 0;
}
int IsEmpty(Stack S) {
	return S->top == -1 ? 1 : 0;
}
void Push(Stack S, int item) {
	S->data[++S->top] = item;
}
int Pop(Stack S) {
	return S->data[S->top--];
}
void AddQ(int item,Stack Ssmall,Stack Sbig) {
	if (!IsFull(Ssmall)) Push(Ssmall, item);
	else if (IsEmpty(Sbig)) {
		while (!IsEmpty(Ssmall)) {
			Push(Sbig, Pop(Ssmall));
		}
		Push(Ssmall, item);
	}
	else {
		printf("ERROR:Full\n");
	}
}
void DeleteQ(Stack Ssmall, Stack Sbig) {
	if (!IsEmpty(Sbig)) printf("%d\n", Pop(Sbig));
	else if (!IsEmpty(Ssmall)) {
		while (!IsEmpty(Ssmall)) {
			Push(Sbig, Pop(Ssmall));
		}
		printf("%d\n", Pop(Sbig));
	}
	else printf("ERROR:Empty\n");
}
int main()
{
	int big, small, t1, t2;
	scanf("%d %d", &t1, &t2);
	big = t1 > t2 ? t1 : t2;
	small = t1 > t2 ? t2 : t1;
	Stack Ssmall = (Stack)malloc(sizeof(struct stack));
	Stack Sbig = (Stack)malloc(sizeof(struct stack));
	Ssmall->MaxSize = small;
	Sbig->MaxSize = big;
	Ssmall->top = -1;
	Sbig->top = -1;
	Ssmall->data = (int *)malloc(Ssmall->MaxSize * sizeof(int));
	Sbig->data = (int *)malloc(Sbig->MaxSize * sizeof(int));
	char temp;
	int t;
	while (1) {
		temp = getchar();
		temp = getchar();
		if (temp == 'T') break;
		if (temp == 'A') {
			scanf("%d", &t);
			AddQ(t, Ssmall, Sbig);
		}
		else {
			DeleteQ(Ssmall, Sbig);
		}
	}
	return 0;
}

测试结果

在这里插入图片描述

问题整理

1.不要想当然地以为输入的数据是从1-n,经验证,也是有0的存在的。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值