循环队列

#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MaxSize 5
typedef int ElemType;
typedef struct
{
	ElemType data[MaxSize];		//数组、存储MaxSize-1个元素
	int front, rear;		//队列头、队列尾下标
}sqQueue;
//初始化
void InitQueue(sqQueue *Q)
{
	Q->rear = Q->front = 0;
}
//判空
int isEmpty(sqQueue *Q)
{
	if (Q->rear == Q->front)
		return 1;
	else
		return 0;
}
//入队
int EnQueue(sqQueue *Q, ElemType x)
{
	if ((Q->rear + 1) % MaxSize == Q->front)
		return 0;
	Q->data[Q->rear] = x;
	Q->rear = (Q->rear + 1) % MaxSize;
	return 1;
}
//出队
int DeQueue(sqQueue *Q, ElemType *x)
{
	if (Q->rear == Q->front)
		return 0;
	*x = Q->data[Q->front];
	Q->front = (Q->front + 1) % MaxSize;
	return 1;
}

int main()
{
	sqQueue Q;
	int ret;			//返回值
	ElemType element;		//出队元素
	InitQueue(&Q);
	ret = isEmpty(&Q);
	if (ret)
	{
		printf("队列为空\n");
	}
	else
	{
		printf("队列不为空\n");
	}
	EnQueue(&Q, 3);
	EnQueue(&Q, 4);
	EnQueue(&Q, 5);
	ret= EnQueue(&Q, 6);
	//因为队列最大长度为MaxSize-1个元素,因此7入队时失败
	ret = EnQueue(&Q, 7);
	if (ret)
	{
		printf("队列为空\n");
	}
	else
	{
		printf("队列不为空\n");
	}
	ret = DeQueue(&Q, &element);
	if (ret)
	{
		printf("出队成功,元素值为%d\n", element);
	}
	else
	{
		printf("出队失败\n");
	}
	ret = DeQueue(&Q, &element);
	if (ret)
	{
		printf("出队成功,元素值为%d\n", element);
	}
	else
	{
		printf("出队失败\n");
	}
	ret = EnQueue(&Q, 8);
	if (ret)
	{
		printf("入队成功\n");
	}
	else
	{
		printf("入队失败\n");
	}
	system("pause");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tefuir_luo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值