C语言 循环队列

#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

#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <assert.h>
#include "cyclequeue.h"


/*----------------------------------------------------
操作目的:   初始化队列
初始条件:   无
操作结果:   
函数参数:
        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);

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值