我是用顺序表写的以后用链表写个
//循环队列的设计与实现
//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));
}
}