diff --git a/SeqQueue.c b/SeqQueue.c new file mode 100644 index 0000000000000000000000000000000000000000..cf6a519f393311267cc6711f81f78d8c5e1cb630 --- /dev/null +++ b/SeqQueue.c @@ -0,0 +1,150 @@ +/* + * @Description: 顺序循环队列 + * @Author: 大熊人 + * @LastEditTime: 2020-11-08 18:27:54 + */ +#include +#include +#include "includes/SeqQueue.h" + +/** + * @description: 初始化顺序循环队列 + * @param {struct *}Q + * @return {int} + */ +int InitQueue(SeqQueue *Q) +{ + Q->base = (QUEUE_DATA_TYPE *)malloc(sizeof(QUEUE_DATA_TYPE) * MAXSIZE); + if (Q->base == NULL) + { + return FALSE; //申请内存空失败 + } + Q->front = 0; + Q->rear = 0; + return TRUE; +} + +/** + * @description: 判断队列是否为空 + * @param {struct *}Q + * @return {int} + */ +int IsEmpty(SeqQueue *Q) +{ + if (QueueLength(Q) == 0) + { + return TRUE; + } + else + { + return FALSE; + } +} + +/** + * @description: 判断队列是否满 + * @param {struct *}Q + * @return {int} + */ +int IsFull(SeqQueue *Q) +{ + if (QueueLength(Q) > 0 && Q->front == Q->rear) + { + return TRUE; + } + else + { + return FALSE; + } +} + +/** + * @description: 入列 + * @param {struct *}Q + * @param {QUEUE_DATA_TYPE *}X + * @return {int} + */ +int EnQueue(SeqQueue *Q, QUEUE_DATA_TYPE X) +{ + if (IsFull(Q) == TRUE) + { + return FALSE; //队列满 + } + Q->base[Q->rear] = X; + Q->rear = (Q->rear + 1) % MAXSIZE; //在MAXSIZE范围内指针后移 + return TRUE; +} + +/** + * @description: 出列 + * @param {struct *}Q + * @param {QUEUE_DATA_TYPE *}X + * @return {int} + */ +int DeQueue(SeqQueue *Q, QUEUE_DATA_TYPE *X) +{ + if (IsEmpty(Q) == TRUE) + { + return FALSE; //队列空 + } + *X = Q->base[Q->front]; + Q->front = (Q->front + 1) % MAXSIZE; //在MAXSIZE范围内指针后移 + return TRUE; +} + +/** + * @description: 获取队头元素 + * @param {struct *}Q + * @return {int} + */ +int GetFront(SeqQueue *Q, QUEUE_DATA_TYPE *X) +{ + if (IsEmpty(Q) == TRUE) + { + return FALSE; + } + *X = Q->base[Q->front]; + return TRUE; +} + +/** + * @description: 获取队列长度 + * @param {struct}Q + * @return {int} + */ +int QueueLength(SeqQueue *Q) +{ + return (Q->rear - Q->front + MAXSIZE) % MAXSIZE; +} + +/** + * @description: 测试顺序循环队列 + */ +void TestSeqQueue() +{ + SeqQueue Q; + QUEUE_DATA_TYPE value; + + if (InitQueue(&Q)) + { + printf("初始化成功!\n"); + } + else + { + printf("初始化失败!\n"); + } + printf("EnQueue:1 %s\n", EnQueue(&Q, 1) ? "TURE" : "FALSE"); + printf("EnQueue:2 %s\n", EnQueue(&Q, 2) ? "TURE" : "FALSE"); + printf("EnQueue:3 %s\n", EnQueue(&Q, 3) ? "TURE" : "FALSE"); + + printf("GetFront:%d %s\n", value, GetFront(&Q, &value) ? "TURE" : "FALSE"); + + while (DeQueue(&Q, &value)) + { + printf("DeQueue:%d\n", value); + } + + printf("GetFront: %s\n", GetFront(&Q, &value) ? "TURE" : "FALSE"); + + free(Q.base); //释放申请的内存空间 +} \ No newline at end of file diff --git a/includes/SeqQueue.h b/includes/SeqQueue.h new file mode 100644 index 0000000000000000000000000000000000000000..e96d5d63dc380d2f97820fd3879516f14d6c8a86 --- /dev/null +++ b/includes/SeqQueue.h @@ -0,0 +1,26 @@ +/* + * @Description: 顺序循环队列 + * @Author: 大熊人 + * @LastEditTime: 2020-11-08 18:28:38 + */ +#define QUEUE_DATA_TYPE int +#define MAXSIZE 10 +#define TRUE 1 +#define FALSE 0 + +/* 顺序循环队列结构体 */ +typedef struct SeqQueue +{ + QUEUE_DATA_TYPE *base; + int front; //头指针 + int rear; //尾指针 +} SeqQueue; + +int InitQueue(SeqQueue *Q); +int IsEmpty(SeqQueue *Q); +int IsFull(SeqQueue *Q); +int EnQueue(SeqQueue *Q, QUEUE_DATA_TYPE X); +int DeQueue(SeqQueue *Q, QUEUE_DATA_TYPE *X); +int GetFront(SeqQueue *Q, QUEUE_DATA_TYPE *X); +int QueueLength(SeqQueue *Q); +void TestSeqQueue(); \ No newline at end of file