循环队列 队列的顺序表示和实现

#include<stdio.h>
#include<stdlib.h>
#define MAXQSIZE 100
typedef struct//构造循环队列数据类型
{	int *base;//队列数组指针
	int front;
	int rear;
}CircleQueue;
void InitQueue(CircleQueue &Q);//初始化创造循环队表
void QueueLength(CircleQueue Q);//输出队列长度
void EnQueue(CircleQueue &Q);//插入元素
void DeQueue(CircleQueue &Q);//删除元素
void ExportQueue(CircleQueue &Q);//输出循环队列
void main()
{	int x;
	CircleQueue Qa;
	printf("1.Insert an element to the CircleQueue.\n");
	printf("2.Delete an element of the CircleQueue.\n");
	printf("3.Export the length of the CircleQueue.\n");
	printf("4.Export the element of the CircleQueue.\n");
	printf("0.End up the operation.\n");
	InitQueue(Qa);
	while(1)
	{	printf("Please input your choice:");
		scanf("%d",&x);
		switch(x)
		{	case 1:	EnQueue(Qa);
					break;
			case 2:	DeQueue(Qa);
					break;
			case 3:	QueueLength(Qa);
					break;
			case 4: ExportQueue(Qa);
					break;
		}
		if(x==0)
		{	printf("The operation in the end!\n");
			break;
		}
	}
}
void InitQueue(CircleQueue &Q)
{	Q.base=(int *)malloc(MAXQSIZE*sizeof(int));
	if(!Q.base)
	{	printf("Fail to create CircleQueue!\n");
		return;
	}
	Q.front=Q.rear=0;
	printf("Succeed to create CircleQueue!\n");
}
void QueueLength(CircleQueue Q)
{	printf("The length of the CircleQueue is %d\n",(Q.rear-Q.front+MAXQSIZE)%MAXQSIZE);
}
void EnQueue(CircleQueue &Q)
{	int n;
	if((Q.rear+1)%MAXQSIZE==Q.front)
	{	printf("The CircleQueue is full!\n");
		return;
	}
	printf("Please input the number to insert:");
	scanf("%d",&n);
	Q.base[Q.rear]=n;
	Q.rear=(Q.rear+1)%MAXQSIZE;//使Q.rear不会超出数组的下标界限,实现队列的循环
	printf("Succeed to insert %d\n",n);
}
void DeQueue(CircleQueue &Q)
{	if(Q.rear==Q.front)
	{	printf("The CircleQueue is empty!\n");
		return;
	}
	printf("Succeed to delete %d\n",Q.base[Q.front]);
	Q.front=(Q.front+1)%MAXQSIZE;
}
void ExportQueue(CircleQueue &Q)
{	CircleQueue p;
	p=Q;
	printf("The CircleQueue is:");
	while(p.front!=p.rear)
	{	printf("%d",p.base[p.front]);
		p.front=(p.front+1)%MAXQSIZE;
	}
	printf("\n");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值