目录
前言
已完成内容
循环队列实现
01-开发环境
02-文件布局
03-代码
01-主函数
02-头文件
03-QueueCommon.cpp
04-QueueFunction.cpp
结语
前言
此专栏包含408考研数据结构全部内容,除其中使用到C++引用外,全为C语言代码。使用C++引用主要是为了简化指针的使用,避免二重指针的出现。
已完成内容
[数据结构]:01-顺序表(C语言实现)_Chandni.的博客-CSDN博客
[数据结构]:02-单链表(C语言实现)_Chandni.的博客-CSDN博客
[数据结构]:03-栈(C语言实现)_Chandni.的博客-CSDN博客
[数据结构]:04-循环队列(数组)(C语言实现)_Chandni.的博客-CSDN博客
循环队列实现
01-开发环境
语言:C/C++14
编译器:MinGW64
集成开发环境:CLion2022.1.3
02-文件布局
请在CLion集成开发环境中创建C++可执行程序,否则无法运行,原因上面已解释。
03-代码
01-主函数
用于测试和初始化队列。
#include "./Head/QueueData.h"
#include "./Source/QueueCommon.cpp"
#include "./Source/QueueFunction.cpp"int main() {LinkedQueue LQ;// 初始化InitializeQueue(LQ);// 入队QueuePush(LQ, 1);QueuePush(LQ, 2);QueuePush(LQ, 3);QueuePrint(LQ);printf("-------------------------\n");// 出队ElemType value;QueuePop(LQ, value);printf("Queue Pop Value = %d\n", value);QueuePop(LQ, value);printf("Queue Pop Value = %d\n", value);QueuePrint(LQ);printf("-------------------------\n");// 入队QueuePush(LQ, 2);QueuePush(LQ, 4);QueuePush(LQ, 5);QueuePush(LQ, 6);QueuePush(LQ, 7);QueuePush(LQ, 8);QueuePrint(LQ);printf("-------------------------\n");return 0;
}
02-头文件
用于存储结构体和常量等。
//
// Created by 24955 on 2023-02-26.
//#ifndef INC_01_ARRAYQUEUE_QUEUEDATA_H
#define INC_01_ARRAYQUEUE_QUEUEDATA_H
// 头文件
#include <stdio.h>
#include <stdlib.h>// 常量
typedef int ElemType;// 结构体
// 结点
typedef struct LinkedNode {ElemType data;struct LinkedNode *next;
} LinkedNode;
// 队列
typedef struct {struct LinkedNode *front, *rear;
} LinkedQueue;
#endif //INC_01_ARRAYQUEUE_QUEUEDATA_H
03-QueueCommon.cpp
用于存储公共函数以及队列的输出。
//
// Created by 24955 on 2023-02-26.
//
// 初始化队列
void InitializeQueue(LinkedQueue &LQ) {LQ.front = LQ.rear = (LinkedNode *) malloc(sizeof(LinkedNode));LQ.front->next = LQ.front;
}// 判断队列是否为空
bool JudgeQueueEmpty(LinkedQueue LQ) {if (LQ.front == LQ.rear) {return true;} else {return false;}
}// 判断队列是否已满
bool JudgeQueueFull(LinkedQueue LQ) {if (LQ.rear->next == LQ.front) {return true;} else {return false;}
}// 队列输出
void QueuePrint(LinkedQueue LQ) {if (!JudgeQueueEmpty(LQ)) {do {LQ.front = LQ.front->next;printf("%3d", LQ.front->data);} while (LQ.front != LQ.rear);printf("\n");} else {printf("Queue Empty.\n");}
}
04-QueueFunction.cpp
用于存储入队、出队等函数。
//
// Created by 24955 on 2023-02-26.
//
// 入队
void QueuePush(LinkedQueue &LQ, ElemType value) {if (JudgeQueueFull(LQ)) {LinkedNode *NewNode = (LinkedNode *) malloc(sizeof(LinkedNode));NewNode->data = value;NewNode->next = LQ.front;LQ.rear->next = NewNode;LQ.rear = NewNode;} else {LQ.rear->next->data = value;LQ.rear = LQ.rear->next;}
}// 出队
void QueuePop(LinkedQueue &LQ, ElemType &value) {if (!JudgeQueueEmpty(LQ)) {LinkedNode *Node = LQ.front->next;value = Node->data;LQ.front = Node;} else {printf("Queue Empty.\n");}
}
结语
本章循环队列的实现形式为链表的实现形式,循环队列还可以使用数组形式实现,数组实现形式请关注本专栏上一章内容。
此博客主要用于408考研数据结构C语言实现记录,内有不足,可留言,可讨论。