数据结构基础代码04顺序循环队列

#include<iostream>
#include<stdlib.h>
#define MaxSize 50
using namespace std;
typedef int ElemType;
//定义 
typedef struct{
	ElemType data[MaxSize];
	int front,rear;
}SqQueue;
//初始化
void InitQueue(SqQueue &Q)
{
	Q.front=Q.rear=0;
}
//判断队列是否为空
bool QueueEmpty(SqQueue Q)
{
	if(Q.front==Q.rear)
		return true;
	else
		return false;
}
//元素入队
bool EnQueue(SqQueue &Q,ElemType x)
{
	if((Q.rear+1)%MaxSize==Q.front)
	{
		return false;
	}//队满 
	Q.data[Q.rear] = x;
	Q.rear=(Q.rear+1)%MaxSize;
	return true;
}
//元素出队
bool DeQueue(SqQueue &Q,ElemType &x)
{
	if(Q.rear==Q.front)
	{
		return false;
	}//队空 
	x=Q.data[Q.front];
	Q.front=(Q.front+1)%MaxSize;
	return true;
}
int main()
{
	SqQueue hahaha;
	InitQueue(hahaha);
	if(QueueEmpty(hahaha))
	cout<<"queue is empty"<<endl;
	else 
	cout<<"queue is not empty"<<endl;
	//试验入队 
	for(int i = 0;i<=5;i++)
	{
		EnQueue(hahaha,i);
	}
	for(int i = 0;i<=5;i++)
	{
		cout<<hahaha.data[i];
	}
	if(QueueEmpty(hahaha))
	cout<<"queue is empty"<<endl;
	else 
	cout<<"queue is not empty"<<endl;
//试验出队,展示过程 
	for(int i = 0;i<=5;i++)
	{
		DeQueue(hahaha,i);//调用出队 
		int cnt = (hahaha.rear-hahaha.front+MaxSize)%MaxSize;//求个数 
		int num = hahaha.front;//记录队头 
		while(cnt)//个数就是输出次数 
		{
			cout<<hahaha.data[num]; //输出队头 
			cnt--;//次数每次减一,cnt=0时退出while 
			num++;//向后移,输出 
		}
		cout<<endl;
	}
	if(QueueEmpty(hahaha))
	cout<<"queue is empty"<<endl;
	else 
	cout<<"queue is not empty"<<endl;
} 

效果图如下:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值