数据结构与算法系列-队列-循环队列

定义:

循环队列:

循环队列是将存储队列的存储区看成是一个收尾相连的环,即将表示队列的数组元素element[0]element[MAXLEN-1]连接起来,形成一个环形表。

在循环队列中 容量设为MANLEN,对首指针为front 队尾指针为rear rear=front时 不能判定循环队列是满还是为空,对此做出规定 front = rear 是循环队列为空的标志.(rear+!)%MAXNLEN = front 是循环队列队满的标志。



#include<stdio.h>
#define MAXLEN 10
typedef int elementtype;
typedef struct{
	elementtype element[MAXLEN];
	int front,rear;
}CQueue;

CQueue InitCQueue(){/*初始化一个空的循环队列*/
	CQueue cq;
	cq.front = 0;
	cq.rear = 0;
	return(cq);
}

int EnCqueue(CQueue *cq,elementtype x){/*若队列未满 入队*/
	if((cq->rear+1)%MAXLEN == cq->front)//队满
		return (0);
	else{
		cq ->rear = (cq->rear+1)%MAXLEN; 
		cq ->element[cq->rear] = x;
		return(1);
	}
}

int DelCqueue(CQueue *cq,elementtype *x){//出队
	if(cq->rear == cq->front)//队空
		return (0);
	else{
		cq ->front = (cq->front+1)%MAXLEN; 
		*x = cq->element[cq->front];
		return(1);
	}
}

void print(CQueue cq){
	int i;
	if(cq.front!=cq.rear){//如果队列非空
		printf("Output elements of cqueue: ");
		i = cq.front;
		do{
			i=(i+1)%MAXLEN;
			printf("%d ",cq.element[i]);
		}while(i!=cq.rear);
	}else{
		printf("THE CQUEUE IS EMPTY!!");
	}
}


main(){
	CQueue cqueue;
	int i;
	elementtype y,z;
	cqueue = InitCQueue();
	printf("\n Adding 9 elements to cqueue: ");
	for(i =1;i<=9;i++){
		scanf("%d",&y);
		EnCqueue(&cqueue,y);
	}
	print(cqueue);
	printf("\n Delete 5 elements from cuqeue: ");

	for(i =1;i<=5;i++){
		DelCqueue(&cqueue,&z);
		printf("%d",z);
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值