2021-08-18王道 数据结构 p90 第4题

第3章 栈和队列

3.3 栈和队列的应用

综合应用题 第4题

在这里插入图片描述

#include <stdio.h>  
#include <stdlib.h>                        //malloc所在头文件
#include <iostream> 
#define MaxSize 100
using namespace std;
typedef int ElemType;
typedef struct{ //声明队列 
	ElemType data[MaxSize];                      
	int front,rear;                 
}SqQueue;
void InitQueue(SqQueue &Q)//初始化队列 
{
	Q.rear=Q.front=0;
}
int EnQueuel(SqQueue &Q,ElemType x)//入队 
{
	if(Q.rear==MaxSize)
	return 0;
	Q.data[Q.rear]=x;
	Q.rear++;
	return 1;
}
int DeQueuel(SqQueue &Q)//出队 
{
	int x=0;
	if(Q.front==Q.rear)
	return 0;
	x=Q.data[Q.front];
	Q.front++;
	return x;
}
bool QueueEmpty(SqQueue &Q)
{
	if(Q.front==Q.rear) 
	return true;
	else
	return false;
} 
void PrintQueue(SqQueue Q) 
{
	while(Q.front!=Q.rear)
	{
		cout<<Q.data[Q.front]<<" ";
		Q.front++;
	}
	cout<<endl;
}

int main() {
    SqQueue q,q1,q2;
    InitQueue(q);
    InitQueue(q1);
    InitQueue(q2);
    for(int i=1;i<=5;i++)
    {
    	EnQueuel(q1,i);
    	EnQueuel(q2,i);
    }
    int i=0,j=0,x;
    while(j<10)
    {
    	if(!QueueEmpty(q1)&&i<4)
    	{
    		x=DeQueuel(q1);
    		EnQueuel(q,x);
    		i++;
    		j++;
    	}
    	else if(!QueueEmpty(q2)&&i==4)
    	{
    		x=DeQueuel(q2);
    		EnQueuel(q,x);
    		j++;
    		i=0;
    	}
    	else
    	{
    		while(!QueueEmpty(q2)&&j<10&&i<4)
    		{
    			x=DeQueuel(q2);
    		    EnQueuel(q,x);
    		    i++;
    		    j++;
    		}
    		i=0;
    	}
    	if(QueueEmpty(q1)&&QueueEmpty(q2))
    	j=11;
    } 
    
    PrintQueue(q);
    PrintQueue(q1);
    PrintQueue(q2);
	return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值