2021-08-17王道 数据结构 p80 第3题

第3章 栈和队列

3.2 队列

综合应用题 第3题

在这里插入图片描述

#include <stdio.h>  
#include <stdlib.h>                        //malloc所在头文件
#include <iostream> 
#define MaxSize 10
using namespace std;
typedef int ElemType;
typedef struct{//声明栈 
	ElemType stack[MaxSize];                      
	int top;                    
}SqStack;
 
void InitStack(SqStack &S)//初始化栈 
{
	S.top=-1;
}

int Push(SqStack &S,ElemType x)//入栈 
{
	if(S.top==MaxSize-1)
	{
		cout<<"栈满"<<endl; 
		exit(0);
	}
	S.stack[++S.top]=x;
	return 1;
}
int Pop(SqStack &S,ElemType &x)//出栈 
{
	if(S.top==-1)
	{
		cout<<"栈空"<<endl;
		return 0;
	}
	x=S.stack[S.top--];
	return 1;
}

bool StatckEmpty(SqStack &S)//判断栈是否为空 
{
	if(S.top==-1) 
	return true;
	else
	return false;
}  

bool StatckOverFlow(SqStack &S)//判断栈是否满 
{
	if(S.top==MaMaxSize-1) 
	return true;
	else
	return false;
}  

int EnQueuel(SqStack &S1,SqStack &S2,ElemType x)//入队 
{
	if(!StatckOverFlow(S1))
	{
		Push(S1,x);
		return 1;
	}
	else if(StatckEmpty(S2))
	{
		while(!StatckEmpty(S1))
		{
			Pop(S1,x);
			Push(S2,x);
		}
		Push(S1,x);
		return 1;
	}
	else
	{
		cout<<"队列已满!"<<endl;
		return 0; 
	}

}
void DeQueuel(SqQueue &Q)//出队 
{
	if(!StatckEmpty(S2))
	{
		Pop(S2,x);
	}
	else if(!StatckEmpty(S1))
	{
		while(!StatckEmpty(S1))
		{
			Pop(S1,x);
			Push(S2,x);
		}
		Pop(S2,x);
	}
	else
		cout<<"队列为空!"<<endl;
}
bool QueueEmpty(SqQueue &Q)
{
	if(StatckEmpty(S1)&&StatckEmpty(S2)) 
	return true;
	else
	return false;

} 
int main() {
	SqStack S1,S2;
    InitStack(S1);
    InitStack(S2);
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值