目录
前言
已完成内容
循环队列实现
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博客
循环队列实现
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() {
    ArrayQueue Q;
    // 初始化
    InitializationQueue(Q);
    // 入队
    QueuePush(Q, 1);
    QueuePush(Q, 2);
    QueuePush(Q, 3);
    QueuePush(Q, 4);
    QueuePush(Q, 5);
    QueuePrint(Q);
    printf("---------------------\n");
    // 出队
    ElemType value;
    QueuePop(Q, value);
    printf("Queue Pop Value = %d\n", value);
    QueuePop(Q, value);
    printf("Queue Pop Value = %d\n", value);
    QueuePrint(Q);
    printf("---------------------\n");
    // 入队
    QueuePush(Q, 4);
    QueuePush(Q, 5);
    QueuePrint(Q);
    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>
// 常量
#define MaxSize 5
typedef int ElemType;
// 结构体
typedef struct {
    ElemType data[MaxSize];
    int front, rear;
} ArrayQueue;
#endif //INC_01_ARRAYQUEUE_QUEUEDATA_H
03-QueueCommon.cpp
用于存储公共函数以及队列的输出。
//
// Created by 24955 on 2023-02-26.
//
// 初始化队列
void InitializationQueue(ArrayQueue &Queue) {
    /*
     * 1. 初始化队列*/
    Queue.front = 0;
    Queue.rear = 0;
}
// 判断队列是否为空
bool JudgeQueueEmpty(ArrayQueue Queue) {
    /*
     * 1. 头指针和尾指针相等则队列为空
     * 2. 这里的指针加引号,只是一种标识,这样说方便理解*/
    if (Queue.front == Queue.rear) {
        return true;
    } else {
        return false;
    }
}
// 判断队列是否已满
bool JudgeQueueFull(ArrayQueue Queue) {
    /*
     * 1. 尾指针+1取模与头指针相等则满*/
    if ((Queue.rear + 1) % MaxSize == Queue.front) {
        return true;
    } else {
        return false;
    }
}
// 输出队列元素
void QueuePrint(ArrayQueue Queue) {
    /*
     * 1. 判断队列是否为空
     * 2. 若不为空则从头输出*/
    if (!JudgeQueueEmpty(Queue)) {
        while (Queue.front != Queue.rear) {
            printf("%3d", Queue.data[Queue.front]);
            Queue.front = (Queue.front + 1) % MaxSize;
        }
        printf("\n");
    } else {
        printf("Queue Empty.\n");
    }
}04-QueueFunction.cpp
用于存储入队、出队等函数。
//
// Created by 24955 on 2023-02-26.
//
// 入队
void QueuePush(ArrayQueue &Queue, ElemType value) {
    /*
     * 1. 判断队列是否已满
     * 2. 若不满则入队*/
    if (!JudgeQueueFull(Queue)) {
        Queue.data[Queue.rear] = value;
        Queue.rear = (Queue.rear + 1) % MaxSize;
    } else {
        printf("Queue Full.\n");
    }
}
// 出队
void QueuePop(ArrayQueue &Queue, ElemType &value) {
    /*
     * 1. 判断队列是否已空
     * 2. 若非空则出队*/
    if (!JudgeQueueEmpty(Queue)) {
        value = Queue.data[Queue.front];
        Queue.front = (Queue.front + 1) % MaxSize;
    } else {
        printf("Queue Empty.\n");
    }
}结语
本章循环队列的实现形式为数组的实现形式,循环队列还可以使用链表形式实现,链表实现形式请关注本专栏下一章。
此博客主要用于408考研数据结构C语言实现记录,内有不足,可留言,可讨论。



















