This repository has been archived on 2024-05-05. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
2023-10-18 21:36:42 +08:00

91 lines
1.8 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
*@文件:queue.c
*@作者:‘你遇了我’
*@time:2022/11/13
*@联系:QQ:321640253
*@版本:V1.3
*@描述:顺序泛型队列
使用案例:
*/
//
// Created by 86186 on 2022/11/13.
//
#include "queue.h"
/*
* @简介:初始化一个队列
* @参数:
* *Q队列指针
* Maxsize队列容器数组的长度
* NodeSize:队列容器数组每个元素的空间
* *container容器数组指针
* @返回值:无
* */
void InitQuecu(SqQuecu *Q,uint16_t Maxsize,uint16_t NodeSize){
Q->front=Q->rear=-1;
Q->size=0;
Q->Maxsize=Maxsize;
Q->NodeSize=NodeSize;
Q->container=calloc(Maxsize,Q->NodeSize);
}
/*
* @简介:判断队列为空
* @参数:队列指针
* @返回值:布尔值
* */
bool QueueEmpty(SqQuecu *Q){
if(Q->size==0)return true;
else return false;
}
/*
* @简介:判断队列为满
* @参数:队列指针
* @返回值:布尔值
* */
bool QueueFull(SqQuecu *Q){
if(Q->size==Q->Maxsize)return true;
else return false;
}
/*
* @简介:进队
* @参数:队列指针
* 进队元素
* @返回值:可进队空间的地址
* */
void *enQueue(SqQuecu *Q){
if(QueueFull(Q))return NULL;
if(Q->rear==Q->Maxsize-1)Q->rear=-1;
Q->rear++;
Q->size++;
return Q->container + Q->rear*Q->NodeSize;
}
/*
* @简介:出队
* @参数:队列指针
* @返回值:出队元素的地址指针
* */
void *deQueue(SqQuecu *Q){
if(QueueEmpty(Q))return NULL;
if(Q->front==Q->Maxsize-1)Q->front=-1;
Q->front++;
Q->size--;
return Q->container+Q->front*Q->NodeSize;
}
/*
* @简介:销毁队列
* @参数:队列指针
*/
void DestroyQueue(SqQuecu *Q){
free(Q->container);
Q->front=Q->rear=Q->size=Q->Maxsize=Q->NodeSize=0;
Q->container=NULL;
}