深圳梵高網(wǎng)站建設(shè)服務(wù)域名購(gòu)買哪個(gè)網(wǎng)站好
目錄
前言
已完成內(nèi)容
循環(huán)隊(duì)列實(shí)現(xiàn)
01-開發(fā)環(huán)境
02-文件布局
?03-代碼
01-主函數(shù)
02-頭文件
03-QueueCommon.cpp
04-QueueFunction.cpp
結(jié)語(yǔ)
前言
? ? ? ? 此專欄包含408考研數(shù)據(jù)結(jié)構(gòu)全部?jī)?nèi)容,除其中使用到C++引用外,全為C語(yǔ)言代碼。使用C++引用主要是為了簡(jiǎn)化指針的使用,避免二重指針的出現(xiàn)。
已完成內(nèi)容
[數(shù)據(jù)結(jié)構(gòu)]:01-順序表(C語(yǔ)言實(shí)現(xiàn))_Chandni.的博客-CSDN博客
[數(shù)據(jù)結(jié)構(gòu)]:02-單鏈表(C語(yǔ)言實(shí)現(xiàn))_Chandni.的博客-CSDN博客
[數(shù)據(jù)結(jié)構(gòu)]:03-棧(C語(yǔ)言實(shí)現(xiàn))_Chandni.的博客-CSDN博客
循環(huán)隊(duì)列實(shí)現(xiàn)
01-開發(fā)環(huán)境
????????語(yǔ)言:C/C++14
????????編譯器:MinGW64
????????集成開發(fā)環(huán)境:CLion2022.1.3
02-文件布局
? ? ? ? 請(qǐng)?jiān)贑Lion集成開發(fā)環(huán)境中創(chuàng)建C++可執(zhí)行程序,否則無(wú)法運(yùn)行,原因上面已解釋。
????????????????????????
?03-代碼
01-主函數(shù)
? ? ? ? 用于測(cè)試和初始化隊(duì)列。
#include "./Head/QueueData.h"
#include "./Source/QueueCommon.cpp"
#include "./Source/QueueFunction.cpp"int main() {ArrayQueue Q;// 初始化InitializationQueue(Q);// 入隊(duì)QueuePush(Q, 1);QueuePush(Q, 2);QueuePush(Q, 3);QueuePush(Q, 4);QueuePush(Q, 5);QueuePrint(Q);printf("---------------------\n");// 出隊(duì)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");// 入隊(duì)QueuePush(Q, 4);QueuePush(Q, 5);QueuePrint(Q);printf("---------------------\n");return 0;
}
02-頭文件
? ? ? ? 用于存儲(chǔ)結(jié)構(gòu)體和常量等。
//
// 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;// 結(jié)構(gòu)體
typedef struct {ElemType data[MaxSize];int front, rear;
} ArrayQueue;
#endif //INC_01_ARRAYQUEUE_QUEUEDATA_H
03-QueueCommon.cpp
? ? ? ? 用于存儲(chǔ)公共函數(shù)以及隊(duì)列的輸出。
//
// Created by 24955 on 2023-02-26.
//
// 初始化隊(duì)列
void InitializationQueue(ArrayQueue &Queue) {/** 1. 初始化隊(duì)列*/Queue.front = 0;Queue.rear = 0;
}// 判斷隊(duì)列是否為空
bool JudgeQueueEmpty(ArrayQueue Queue) {/** 1. 頭指針和尾指針相等則隊(duì)列為空* 2. 這里的指針加引號(hào),只是一種標(biāo)識(shí),這樣說方便理解*/if (Queue.front == Queue.rear) {return true;} else {return false;}
}// 判斷隊(duì)列是否已滿
bool JudgeQueueFull(ArrayQueue Queue) {/** 1. 尾指針+1取模與頭指針相等則滿*/if ((Queue.rear + 1) % MaxSize == Queue.front) {return true;} else {return false;}
}// 輸出隊(duì)列元素
void QueuePrint(ArrayQueue Queue) {/** 1. 判斷隊(duì)列是否為空* 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
? ? ? ? 用于存儲(chǔ)入隊(duì)、出隊(duì)等函數(shù)。
//
// Created by 24955 on 2023-02-26.
//
// 入隊(duì)
void QueuePush(ArrayQueue &Queue, ElemType value) {/** 1. 判斷隊(duì)列是否已滿* 2. 若不滿則入隊(duì)*/if (!JudgeQueueFull(Queue)) {Queue.data[Queue.rear] = value;Queue.rear = (Queue.rear + 1) % MaxSize;} else {printf("Queue Full.\n");}
}// 出隊(duì)
void QueuePop(ArrayQueue &Queue, ElemType &value) {/** 1. 判斷隊(duì)列是否已空* 2. 若非空則出隊(duì)*/if (!JudgeQueueEmpty(Queue)) {value = Queue.data[Queue.front];Queue.front = (Queue.front + 1) % MaxSize;} else {printf("Queue Empty.\n");}
}
結(jié)語(yǔ)
? ? ? ?本章循環(huán)隊(duì)列的實(shí)現(xiàn)形式為數(shù)組的實(shí)現(xiàn)形式,循環(huán)隊(duì)列還可以使用鏈表形式實(shí)現(xiàn),鏈表實(shí)現(xiàn)形式請(qǐng)關(guān)注本專欄下一章。
????????此博客主要用于408考研數(shù)據(jù)結(jié)構(gòu)C語(yǔ)言實(shí)現(xiàn)記錄,內(nèi)有不足,可留言,可討論。