循环队列的设计与实现

我是用顺序表写的以后用链表写个


//循环队列的设计与实现
//2012-12-18
//author:@quanwei


#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
typedef struct{
	int *elem;
	int front;
	int rear;
}QUEUE;


/*********************/
//函数名:InitQueue(QUEUE *queue)
//参数:QUEUE *queue 队列指针
//作用:建立长为队列MAXSIZE-1的循环队列
//返回值:无
/*********************/
void InitQueue(QUEUE *queue){
	queue->elem = (int *)malloc(sizeof(int)*MAXSIZE);
	if(queue->elem == NULL){
		printf("OVERFLOW");
		exit(0);
	}
	queue->front = queue->rear = 0;
}


/*********************/
//函数名:EnQueue(QUEUE *queue,int elem)
//参数:QUEUE *queue 队列指针 
//		int elem 入队元素
//作用:将elem 入队
//返回值:无
/*********************/
void EnQueue(QUEUE *queue,int elem){
	queue->rear = (queue->rear + 1) % MAXSIZE;
	if(queue->rear == queue->front){
		printf("The queue is full");
		exit(0);
	}
	queue->elem[queue->rear] = elem;
}


/*********************/
//函数名:DeQueue(QUEUE *queue)
//参数:QUEUE *queue 队列指针 
//作用:将队头元素出队
//返回值:队头元素
/*********************/
int DeQueue(QUEUE *queue){
	if(queue->front == queue->rear){
		printf("The queue is empty");
		exit(0);
	}
	queue->front = (queue->front + 1)%MAXSIZE;
	return queue->elem[queue->front];
}


/*********************/
//函数名:QueueGet(QUEUE *queue)
//参数:QUEUE *queue 队列指针 
//作用:取队头元素
//返回值:队头元素
/*********************/
int QueueGet(QUEUE *queue){
	if(queue->front == queue->rear){
		printf("The queue is empty");
		exit(0);
	}
	int t = (queue->front + 1)%MAXSIZE;		//临时存放front的值
	return queue->elem[t];
}


/****************	测试程序	***************/
void main(){
	QUEUE *queue = (QUEUE *)malloc(sizeof(QUEUE));			//定义队列
	char ch = 'y';	
	int num;
	InitQueue(queue);
	while(ch == 'y'){
		printf("Input element");
		scanf("%d",&num);
		fflush(stdin);
		EnQueue(queue,num);
		printf("go on?(y/n)");
		scanf("%c",&ch);
		fflush(stdin);
	}
	printf("The front element of the stack is:%4d\n",QueueGet(queue));
	printf("now pop queue\n");
	while(queue->front != queue->rear){
		printf("%4d",DeQueue(queue));
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值