線性結構常見應用之隊列[基于郝斌課程]
定義:
??一種可以實現“先進先出”的存儲結構
??隊列類似于排隊買票
分類:
??鏈式隊列:基于列表
??靜態隊列:基于數組
????靜態隊列通常都必須是循環隊列
??????靜態隊列為什么是循環隊列?
????????減少對內存的浪費
????????用傳統數組來實現隊列的話,參數只能加不能減
??????循環隊列需要幾個參數來確定以及各個參數的含義
????????需要兩個參數來確定:front 和 rear
????????兩個參數不同場合有不同的含義
??????????隊列初始化: front 和 rear 都是0
??????????隊列非空:front 代表的是隊列的第一個元素, rear 代表的是隊列的最后一個有效元素的下一個元素
??????????隊列空:front 和 rear 是相等的,但是不一致是0
??????循環隊列的入隊偽算法
????????rear = (rear + 1) % 數組的長度
??????循環隊列的出隊偽算法
????????front = (front + 1) % 數組的長度
??????如何判斷循環隊列是否為空?
????????如果 front == rear,則隊列為空
??????如何判斷循環隊列是否已滿?
???????front 和 rear 大小沒有規律,可以 front 比 rear 大,也可以是 front 比 rear 小?
???????規定放入的元素比數組長度少1,只要 rear 和 front 相鄰,則滿了
???????如果 ((rear + 1) % 數組長度) == front,則隊列滿了
隊列的應用:
??所有和時間有關的操作都與隊列有關
/*
@file main.c
@brief 線性結構常見應用之隊列
@author EricsT (EricsT@163.com)
@version v1.0.0
@date 2025-09-24
@history 2025-09-24 EricsT - 新建文件
*/
#include <stdio.h>
#include <malloc.h>
#define LEN 6
typedef struct QUEUE
{
int* pBase;
int front;
int rear;
}Queue, *PtrQueue;
void InitQueue(PtrQueue ptrQueue);//初始化
bool InQueue(PtrQueue ptrQueue, int inValue);//入隊
bool OutQueue(PtrQueue ptrQueue);//出隊
bool isEmptyQueue(const PtrQueue ptrQueue);//判斷是否為空
bool isFullQueue(const PtrQueue ptrQueue);//判斷是否滿了
void TraverseQueue(const PtrQueue ptrQueue);//遍歷
int main(void)
{
Queue queue;
InitQueue(&queue);
if (isEmptyQueue(&queue))
printf("隊列為空\n");
else
printf("隊列不空\n");
int inVaule;
printf("請輸入要入隊的值:");
scanf("%d", &inVaule);
InQueue(&queue, inVaule);
printf("請輸入要入隊的值:");
scanf("%d", &inVaule);
InQueue(&queue, inVaule);
printf("請輸入要入隊的值:");
scanf("%d", &inVaule);
InQueue(&queue, inVaule);
printf("請輸入要入隊的值:");
scanf("%d", &inVaule);
InQueue(&queue, inVaule);
printf("請輸入要入隊的值:");
scanf("%d", &inVaule);
InQueue(&queue, inVaule);
printf("請輸入要入隊的值:");
scanf("%d", &inVaule);
InQueue(&queue, inVaule);
printf("請輸入要入隊的值:");
scanf("%d", &inVaule);
InQueue(&queue, inVaule);
if (isEmptyQueue(&queue))
printf("隊列為空\n");
else
printf("隊列不空\n");
if (isFullQueue(&queue))
printf("隊列已滿\n");
else
printf("隊列未滿\n");
TraverseQueue(&queue);
OutQueue(&queue);
TraverseQueue(&queue);
return 0;
}
void InitQueue(PtrQueue ptrQueue)
{
ptrQueue->pBase = (int*)malloc(sizeof(int) * LEN);
ptrQueue->front = 0;
ptrQueue->rear = 0;
}
bool InQueue(PtrQueue ptrQueue, int inValue)
{
if (isFullQueue(ptrQueue))
return false;
ptrQueue->pBase[ptrQueue->rear] = inValue;
ptrQueue->rear = (ptrQueue->rear + 1) % LEN;//從后面加入
return true;
}
bool OutQueue(PtrQueue ptrQueue)
{
if (isEmptyQueue(ptrQueue))
return false;
ptrQueue->front = (ptrQueue->front + 1) % LEN;//先進先出//從前面出去
return true;
}
bool isEmptyQueue(const PtrQueue ptrQueue)
{
if (ptrQueue->front == ptrQueue->rear)
return true;
return false;
}
bool isFullQueue(const PtrQueue ptrQueue)
{
if (ptrQueue->front == ((ptrQueue->rear + 1) % LEN))
return true;
return false;
}
void TraverseQueue(const PtrQueue ptrQueue)
{
int i = ptrQueue->front;
while (ptrQueue->rear != i)
{
printf("%d ", ptrQueue->pBase[i]);
i = (i + 1) % LEN;
}
printf("\n");
}
本文來自博客園,作者:EricsT,轉載請注明原文鏈接:http://www.rzrgm.cn/EricsT/p/19109274

浙公網安備 33010602011771號