#ifndef _CYCLEQUEUE_H_
#define _CYCLEQUEUE_H_
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define QUEUESIZE 20
typedef int ElemType ;
typedef int State;
typedef struct Queue
{
ElemType data[QUEUESIZE];
int front;
int rear;
int count;
}CycleQueue;
int InitCycleQueue(CycleQueue *Que);
int CycleQueueEmpty(CycleQueue *Que);
int CycleQueueFull(CycleQueue *Que);
int CycleQueueLength(CycleQueue *Que);
int GetCycleHead(CycleQueue *Que, ElemType *e);
int CycleQueueTraverse(CycleQueue *Que, void (*fp)(ElemType));
int ClearCycleQueue(CycleQueue *Que);
int PushCycleQueue(CycleQueue *Que, ElemType *e);
int PopCycleQueue(CycleQueue *Que, ElemType *e);
int PrintCycleQueue(CycleQueue *Que);
#endif
/*----------------------------------------------------
操作目的: 初始化队列
初始条件: 无
操作结果:
函数参数:
CycleQueue *Q 待初始化的队列
返回值:
bool 操作是否成功
----------------------------------------------------*/
int InitLinkQueue(CycleQueue *Que)
{
Que->front = Que->rear = 0;
Que->count = 0;
return TRUE;
}
//判断队列为空和满
//1、使用计数器count,队列为空和满时,front都等于rear
//2、少用一个元素的空间,约定队列满时:(rear+1)%QUEUESIZE=front,为空时front=rear
//rear指向队尾元素的下一个位置,始终为空;队列的长度为(rear-front+QUEUESIZE)%QUEUESIZE
/*************************************************
Function: CycleQueueEmpty
Description: 队列是否为空
Input: 队列指针 CycleQueue *Que
Output:
Return: 为空返回TRUE,否则返回FALSE
Others:
*************************************************/
int CycleQueueEmpty(CycleQueue *Que)
{
if(Que->count == 0)
return TRUE;
else
return FALSE;
}
/*************************************************
Function: CycleQueueFull
Description: 队列是否为满
Input: 队列指针 CycleQueue *queue
Output:
Return: 为满返回TRUE,否则返回FALSE
Others:
*************************************************/
State CycleQueueFull(CycleQueue *Que)
{
if(Que->count == QUEUESIZE)
return TRUE;
else
return FALSE;
}
/*************************************************
Function: PushCycleQueue
Description: 入队
Input: 队列指针 CycleQueue *queue
数据元素 ElemType e
Output:
Return: 成功返回OK,失败返回ERROR
Others:
*************************************************/
int PushCycleQueue(CycleQueue *Que, ElemType *e)
{
//验证队列是否已满
if(Que->count == QUEUESIZE)
{
printf("PushCycleQueue fail: The queue is full\n");
return ERROR;
}
//入队
Que->data[Que->rear] = *e;
//对尾指针后移
Que->rear = (Que->rear + 1) % QUEUESIZE;
//更新队列长度
Que->count++;
return TRUE;
}
/*************************************************
Function: PopCycleQueue
Description: 出队
Input: 队列指针 CycleQueue *Que
Output:
Return: 成功返回数据元素,失败程序退出
Others:
*************************************************/
int PopCycleQueue(CycleQueue *Que , ElemType *e)
{
//判断队列是否为空
if(Que->count == 0)
{
printf("PopCycleQueue fail: The queue is empty!\n");
exit(EXIT_FAILURE);
}
//保存返回值
*e = Que->data[Que->front];
//更新队头指针
Que->front = (Que->front + 1) % QUEUESIZE;
//更新队列长度
Que->count--;
return TRUE;
}
/*************************************************
Function: GetCycleHead
Description: 取队头元素
Input: 队列指针 CycleQueue *Que
Output:
Return: 成功返回数据元素,否则程序退出
Others:
*************************************************/
int GetCycleHead(CycleQueue *Que, ElemType *e)
{
//判断队列是否为空
if(Que->count == 0)
{
printf("The queue is empty!");
exit(EXIT_FAILURE);
}
*e = Que->data[Que->front];
return TRUE;
}
/*************************************************
Function: CycleQueueTraverse
Description: 清空队列
Input: 队列指针 CycleQueue *Que
Output:
Return: 成功返回OK
Others:
*************************************************/
int CycleQueueTraverse(CycleQueue *Que, void (*fp)(ElemType))
{
//判断队列是否为空
if(Que->count == 0)
{
printf("CycleQueueTraverse error :The queue is empty!\n");
exit(EXIT_FAILURE);
}
int x = Que->front;
do{
(*fp)(Que ->data[x]);
x = (x + 1) % QUEUESIZE;
}while(x != Que->rear);
}
/*************************************************
Function: ClearCycleQueue
Description: 清空队列
Input: 队列指针 CycleQueue *Que
Output:
Return: 成功返回OK
Others:
*************************************************/
int ClearCycleQueue(CycleQueue *Que )
{
bzero(Que->data,QUEUESIZE);
Que->front = Que->rear = 0;
Que->count = 0;
return OK;
}
/*************************************************
Function: CycleQueueLength
Description: 取得队列的长度
Input: 队列指针 CycleQueue *Que
Output:
Return: 返回队列的长度
Others:
*************************************************/
int CycleQueueLength(CycleQueue *Que)
{
return Que->count;
}
/*************************************************
Function: PrintCycleQueue
Description: 打印队列
Input: 队列指针 CycleQueue *Que
Output:
Return:
Others:
*************************************************/
int PrintCycleQueue(CycleQueue *Que)
{
//判断队列是否为空
if(Que->count == 0)
{
printf("PrintCycleQueue error :The queue is empty!\n");
exit(EXIT_FAILURE);
}
int x = Que->front;
printf("PrintCycleQueue:");
do{
printf("%d ",Que->data[x]);
x = (x + 1) % QUEUESIZE;
}while(x != Que->rear);
printf("\n");
}
int main(int argc , char *argv[])
{
CycleQueue ue;
InitLinkQueue(&ue);
int i = 1;
for(i;i<=20;i++)
if(0 == PushCycleQueue(&ue,&i))break;
PrintCycleQueue(&ue);
ElemType w;
GetCycleHead(&ue , &w);
printf("GetCycleHead:%d\n",w);
PopCycleQueue(&ue,&w);
PrintCycleQueue(&ue);
GetCycleHead(&ue , &w);
printf("GetCycleHead:%d\n",w);
PushCycleQueue(&ue,&i);
ClearCycleQueue(&ue);
PrintCycleQueue(&ue);
}