数据结构C语言实现之循环队列

#include <stdio.h>
#include <stdlib.h>
//定义函数结果状态码
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0

//定义循环队列空间大小
#define QUEUESIZE 20

//定义数据类型
typedef int ElemType ;
//定义程序返回状态类型
typedef int State;

//循环队列存储结构
typedef struct _CircleQueue
{
    ElemType data[QUEUESIZE];//存储队列元素
    int front;//队列头指针
    int rear;//队列尾指针
    int count;//队列元素个数
}CircleQueue;


/*************************************************
Function:       InitQueue
Description:    初始化,构造空队列
Input:          队列指针 CircleQueue *queue
Output:
Return:         成功返回OK
Others:         空队列 queue->front = queue->rear = 0
*************************************************/
State InitQueue(CircleQueue *queue)
{
    queue->front = queue->rear = 0;
    queue->count = 0;
    return OK;

}

//判断队列为空和满
//1、使用计数器count,队列为空和满时,front都等于rear
//2、少用一个元素的空间,约定队列满时:(rear+1)%QUEUESIZE=front,为空时front=rear
//rear指向队尾元素的下一个位置,始终为空;队列的长度为(rear-front+QUEUESIZE)%QUEUESIZE

/*************************************************
Function:       IsQueueEmpty
Description:    队列是否为空
Input:          队列指针 CircleQueue *queue
Output:
Return:         为空返回TRUE,否则返回FALSE
Others:
*************************************************/
State IsQueueEmpty(CircleQueue *queue)
{
    if(queue->count == 0)
        return TRUE;
    else
        return FALSE;


}

/*************************************************
Function:       IsQueueFull
Description:    队列是否为满
Input:          队列指针 CircleQueue *queue
Output:
Return:         为满返回TRUE,否则返回FALSE
Others:
*************************************************/
State IsQueueFull(CircleQueue *queue)
{
    if(queue->count == QUEUESIZE)
        return TRUE;
    else
        return FALSE;


}

/*************************************************
Function:       EnQueue
Description:    入队
Input:          队列指针 CircleQueue *queue
                数据元素   ElemType e
Output:
Return:         成功返回OK,失败返回ERROR
Others:
*************************************************/
State EnQueue(CircleQueue *queue, ElemType e)
{
    //验证队列是否已满
    if(queue->count == QUEUESIZE)
    {
        printf("The queue is full");
        return ERROR;
    }
    //入队
    queue->data[queue->rear] = e;
    //对尾指针后移
    queue->rear = (queue->rear + 1) % QUEUESIZE;
    //更新队列长度
    queue->count++;
    return OK;

}

/*************************************************
Function:       DeQueue
Description:    出队
Input:          队列指针 CircleQueue *queue
Output:
Return:         成功返回数据元素,失败程序退出
Others:
*************************************************/
ElemType DeQueue(CircleQueue *queue)
{
    //判断队列是否为空
    if(queue->count == 0)
    {
        printf("The queue is empty!");
        exit(EXIT_FAILURE);
    }

    //保存返回值
    ElemType e = queue->data[queue->front];
    //更新队头指针
    queue->front = (queue->front + 1) % QUEUESIZE;
    //更新队列长度
    queue->count--;

    return e;

}

/*************************************************
Function:       GetHead
Description:    取队头元素
Input:          队列指针 CircleQueue *queue
Output:
Return:         成功返回数据元素,否则程序退出
Others:
*************************************************/
ElemType GetHead(CircleQueue *queue)
{
    //判断队列是否为空
    if(queue->count == 0)
    {
        printf("The queue is empty!");
        exit(EXIT_FAILURE);
    }

    return queue->data[queue->front];

}

/*************************************************
Function:       ClearQueue
Description:    清空队列
Input:          队列指针 CircleQueue *queue
Output:
Return:         成功返回OK
Others:
*************************************************/
State ClearQueue(CircleQueue *queue )
{
    queue->front = queue->rear = 0;
    queue->count = 0;
    return OK;

}

/*************************************************
Function:       GetLength
Description:    取得队列的长度
Input:          队列指针 CircleQueue *queue
Output:
Return:         返回队列的长度
Others:
*************************************************/
int GetLength(CircleQueue *queue)
{
    return queue->count;


}

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值