C語言簡明講解隊列的實現(xiàn)方法
前言
大家好啊,我又雙叒叕來水博客了,道路是曲折的,前途是光明的,事物是呈螺旋式上升的,事物最終的發(fā)展結(jié)果還是我們多多少少能夠決定的,好啦,吹水結(jié)束,這與這篇博客的主題并沒有太多聯(lián)系。關(guān)于棧和隊列這一板塊本來是想不寫(就是想偷懶),但是想了想,覺得這樣不太好,關(guān)于數(shù)據(jù)結(jié)構(gòu)這一塊可能會有缺失,所以最終還是決定寫,必須補(bǔ)齊這一塊,恰好最近有時間寫博客,所以還是寫了,這篇博客將介紹隊列的知識點,理解鏈表那一塊的操作后,棧和隊列的相關(guān)操作還是比較容易去理解的。
隊列的表示和實現(xiàn)
隊列的概念及結(jié)構(gòu)
隊列:只允許在一端進(jìn)行插入數(shù)據(jù)操作,在另一端進(jìn)行刪除數(shù)據(jù)操作的特殊線性表,隊列具有先進(jìn)先出FIFO(First in First Out)
入隊列:進(jìn)行插入操作的一端稱為隊尾
出隊列:進(jìn)行刪除操作的一端稱為隊頭
敲黑板,開始摸魚:
其實也沒什么重點啦,都是一些基本的概念性東西,主要有:
1.要理解FIFO代表的意思
2.什么是隊尾隊頭,別弄混了
隊列的實現(xiàn)有兩種方法:
數(shù)組:不適合,隊頭出數(shù)據(jù)需要挪動數(shù)據(jù),一般還會出現(xiàn)假溢出,循環(huán)隊列啥的
鏈表:單鏈表較合適,頭刪尾插,效率較高
當(dāng)然,并不是說一定要用哪種,由于課本是以數(shù)組為例,這里以單鏈表為例
代碼實現(xiàn)
還是老樣子,分為三部分,直接上手代碼,來,跟著我一起敲
Queue.h
#pragma once #include <stdio.h> #include <stdbool.h> #include <assert.h> #include <stdlib.h> typedef int QDataType; typedef struct QueueNode { struct QueueNode* next; QDataType data; }QNode; typedef struct Queue { QNode* head; QNode* tail; }Queue; //初始化 void QueueInit(Queue* pq); //銷毀 void QueueDestory(Queue* pq); //隊尾入 void QueuePush(Queue* pq, QDataType x); //隊頭出 void QueuePop(Queue* pq); //取隊頭數(shù)據(jù) QDataType QueueFront(Queue* pq); //取隊尾數(shù)據(jù) QDataType QueuBack(Queue* pq); //個數(shù) int QueueSize(Queue* pq); //判斷是否為空 bool QueueEmpty(Queue* pq);
Queue.c
#include "Queue.h" void QueueInit(Queue* pq) { assert(pq); pq->head = pq->tail = NULL; } void QueueDestory(Queue* pq) { assert(pq); QNode* cur = pq->head; while (cur) { QNode* next = cur->next; free(cur); cur = next; } pq->head = pq->tail = NULL; } //隊尾入 void QueuePush(Queue* pq, QDataType x) { assert(pq); QNode* newnode = (QNode*)malloc(sizeof(QNode)); if (newnode == NULL) { printf("malloc fail\n"); exit(-1); } newnode->data = x; newnode->next = NULL; if (pq->tail == NULL) { pq->head = pq->tail = newnode; } else { pq->tail->next = newnode; pq->tail = newnode; } } //隊頭出 void QueuePop(Queue* pq) { assert(pq); assert(pq->head); //1.一個(防止野指針) //2.多個 if (pq->head->next == NULL) { free(pq->head); pq->head = pq->tail = NULL; } else { QNode* next = pq->head->next; free(pq->head); pq->head = next; } } QDataType QueueFront(Queue* pq) { assert(pq); assert(pq->head); return pq->head->data; } QDataType QueuBack(Queue* pq) { assert(pq); assert(pq->head); return pq->tail->data; } int QueueSize(Queue* pq) { assert(pq); int size = 0; QNode* cur = pq->head; while (cur) { ++size; cur = cur->next; } return size; } bool QueueEmpty(Queue* pq) { assert(pq); return pq->head == NULL; }
Test.c
#include "Queue.h" void TestQueue() { Queue q; QueueInit(&q); QueuePush(&q, 1); QueuePush(&q, 2); printf("%d ", QueueFront(&q)); QueuePop(&q); QueuePush(&q, 3); QueuePush(&q, 4); while (!QueueEmpty(&q)) { printf("%d ", QueueFront(&q)); QueuePop(&q); } printf("\n"); QueueDestory(&q); } int main() { TestQueue(); return 0; }
束語
關(guān)于堆棧和隊列的相關(guān)操作就說到這里了,如果對你有幫助的話,那就點個贊把!
到此這篇關(guān)于C語言簡明講解隊列的實現(xiàn)方法的文章就介紹到這了,更多相關(guān)C語言隊列內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言?超詳細(xì)梳理總結(jié)動態(tài)內(nèi)存管理
動態(tài)內(nèi)存是相對靜態(tài)內(nèi)存而言的。所謂動態(tài)和靜態(tài)就是指內(nèi)存的分配方式。動態(tài)內(nèi)存是指在堆上分配的內(nèi)存,而靜態(tài)內(nèi)存是指在棧上分配的內(nèi)存,本文帶你深入探究C語言中動態(tài)內(nèi)存的管理2022-03-03關(guān)于C++靜態(tài)成員函數(shù)訪問非靜態(tài)成員變量的問題
靜態(tài)成員函數(shù)不能訪問非靜態(tài)成員,這是因為靜態(tài)函數(shù)屬于類而不是屬于整個對象,靜態(tài)函數(shù)中的 member可能都沒有分配內(nèi)存。靜態(tài)成員函數(shù)沒有隱含的this自變量。所以,它就無法訪問自己類的非靜態(tài)成員2013-10-10C++中priority_queue的使用與模擬實現(xiàn)
本文主要介紹了C++中priority_queue的使用與模擬實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02