網站首頁 編程語言 正文
隊列基本概念
隊列是最常見的概念,日常生活經常需要排隊,仔細觀察隊列會發(fā)現(xiàn),隊列是一種邏輯結構,是一種特殊的線性表。特殊在:
只能在固定的兩端操作線性表
只要滿足上述條件,那么這種特殊的線性表就會呈現(xiàn)出一種“先進先出”的邏輯,這種邏輯就被稱為隊列。
由于約定了只能在線性表固定的兩端進行操作,于是給隊列這種特殊的線性表的插入刪除,起個特殊的名稱:
隊頭:可以刪除節(jié)點的一端
隊尾:可以插入節(jié)點的一端
入隊:將節(jié)點插入到隊尾之后,函數(shù)名通常為enQueue()
出隊:將隊頭節(jié)點從隊列中剔除,函數(shù)名通常為outQueue()
取隊頭:取得隊頭元素,但不出隊,函數(shù)名通常為front()
本題就是手擼數(shù)據(jù)結構中基本的隊列結構,常用的有兩種,一種是用鏈表實現(xiàn),一種是數(shù)組實現(xiàn)。本文將會給出兩種實現(xiàn)方式
1,數(shù)組實現(xiàn)
typedef struct { int value[1000]; int len; } FrontMiddleBackQueue; FrontMiddleBackQueue* frontMiddleBackQueueCreate() { FrontMiddleBackQueue *queue = (FrontMiddleBackQueue *)malloc(sizeof(FrontMiddleBackQueue)); memset(queue,0,sizeof(FrontMiddleBackQueue)); return queue; } void insert(FrontMiddleBackQueue* obj, int pos, int val) { //在pos位置插入val,則pos(從0開始)位置后的數(shù)統(tǒng)一向后挪一個位置,隊列長度加1 int i = 0; for(i=obj->len; i>pos; i--) { obj->value[i] = obj->value[i-1]; } obj->value[pos] = val; obj->len++; } int pop(FrontMiddleBackQueue* obj, int pos) { //彈出pos位置的val,則pos(從0開始)位置后向前統(tǒng)一挪一個位置,隊列長度減一 if(obj->len == 0) return -1; int i = 0; int popval = obj->value[pos]; //先將pos位置的數(shù)保存下來,不然下面的移位操作就覆蓋了pos位置的值 for(i=pos; i<obj->len-1; i++) { obj->value[i] = obj->value[i+1]; } obj->len--; return popval; } void frontMiddleBackQueuePushFront(FrontMiddleBackQueue* obj, int val) { insert(obj,0,val); } void frontMiddleBackQueuePushMiddle(FrontMiddleBackQueue* obj, int val) { insert(obj,obj->len/2,val); } void frontMiddleBackQueuePushBack(FrontMiddleBackQueue* obj, int val) { insert(obj,obj->len,val); } int frontMiddleBackQueuePopFront(FrontMiddleBackQueue* obj) { return pop(obj,0); } int frontMiddleBackQueuePopMiddle(FrontMiddleBackQueue* obj) { return pop(obj,(obj->len-1)/2); } int frontMiddleBackQueuePopBack(FrontMiddleBackQueue* obj) { return pop(obj, obj->len-1); } void frontMiddleBackQueueFree(FrontMiddleBackQueue* obj) { free(obj); } /** * Your FrontMiddleBackQueue struct will be instantiated and called as such: * FrontMiddleBackQueue* obj = frontMiddleBackQueueCreate(); * frontMiddleBackQueuePushFront(obj, val); * frontMiddleBackQueuePushMiddle(obj, val); * frontMiddleBackQueuePushBack(obj, val); * int param_4 = frontMiddleBackQueuePopFront(obj); * int param_5 = frontMiddleBackQueuePopMiddle(obj); * int param_6 = frontMiddleBackQueuePopBack(obj); * frontMiddleBackQueueFree(obj); */
運行結果
?2,鏈表實現(xiàn)
1,設計鏈表結構,鏈表維持一個頭節(jié)點和尾結點,頭節(jié)點始終在最前面并且頭結點的data存儲整個隊列的節(jié)點數(shù),尾結點始終是最后一個節(jié)點
2,設計插入節(jié)點函數(shù)和刪除節(jié)點函數(shù),push和pop操作只需要根據(jù)不同場景傳入不同的參數(shù)即可完成統(tǒng)一的操作
typedef struct tag_Node { int data; struct tag_Node* next, *prev; }Node; typedef struct { Node* front; Node* rear; } FrontMiddleBackQueue; FrontMiddleBackQueue* frontMiddleBackQueueCreate() { FrontMiddleBackQueue* que = (FrontMiddleBackQueue *)malloc(sizeof(FrontMiddleBackQueue)); que->front = (Node *)malloc(sizeof(Node)); que->rear = (Node *)malloc(sizeof(Node)); que->front->data = 0; que->front->next = NULL; que->rear->data = 0; que->rear->next = NULL; que->front->next = que->rear; que->rear->prev = que->front; return que; } void AddNode(FrontMiddleBackQueue* obj, Node *cur, int val) { Node* addNode = (Node *)malloc(sizeof(Node)); addNode->data = val; addNode->prev = cur->prev; addNode->next = cur; cur->prev->next = addNode; cur->prev = addNode; obj->front->data++; return; } Node* GetMiddleNode(FrontMiddleBackQueue* obj, bool isAdd) { Node* tmp = obj->front->next; int len = isAdd ? (obj->front->data / 2) : ((obj->front->data - 1) / 2); for (int i = 0; i < len; i++) { tmp = tmp->next; } return tmp; } void frontMiddleBackQueuePushFront(FrontMiddleBackQueue* obj, int val) { AddNode(obj, obj->front->next, val); return; } void frontMiddleBackQueuePushMiddle(FrontMiddleBackQueue* obj, int val) { AddNode(obj, GetMiddleNode(obj, true), val); return; } void frontMiddleBackQueuePushBack(FrontMiddleBackQueue* obj, int val) { AddNode(obj, obj->rear, val); return; } int RemoveNode(FrontMiddleBackQueue* obj, Node* cur) { if (obj->front->data == 0) { return -1; } cur->next->prev = cur->prev; cur->prev->next = cur->next; obj->front->data--; int item = cur->data; free(cur); return item; } int frontMiddleBackQueuePopFront(FrontMiddleBackQueue* obj) { return RemoveNode(obj, obj->front->next); } int frontMiddleBackQueuePopMiddle(FrontMiddleBackQueue* obj) { return RemoveNode(obj, GetMiddleNode(obj, false)); } int frontMiddleBackQueuePopBack(FrontMiddleBackQueue* obj) { return RemoveNode(obj, obj->rear->prev); } void frontMiddleBackQueueFree(FrontMiddleBackQueue* obj) { while (RemoveNode(obj, obj->front->next) != -1); free(obj->front); free(obj->rear); free(obj); return; } /** * Your FrontMiddleBackQueue struct will be instantiated and called as such: * FrontMiddleBackQueue* obj = frontMiddleBackQueueCreate(); * frontMiddleBackQueuePushFront(obj, val); * frontMiddleBackQueuePushMiddle(obj, val); * frontMiddleBackQueuePushBack(obj, val); * int param_4 = frontMiddleBackQueuePopFront(obj); * int param_5 = frontMiddleBackQueuePopMiddle(obj); * int param_6 = frontMiddleBackQueuePopBack(obj); * frontMiddleBackQueueFree(obj); */
運行結果:
?總結
原文鏈接:https://blog.csdn.net/qq_27071221/article/details/121921450
相關推薦
- 2022-12-05 Go?reflect?反射原理示例詳解_Golang
- 2022-09-21 Android?Flutter繪制有趣的?loading加載動畫_Android
- 2022-12-26 React?tabIndex使非表單元素支持focus和blur事件_React
- 2022-04-30 DataGridView凍結列或行、列順序調整、操作行頭列頭標題的方法_C#教程
- 2022-06-25 pytorch中permute()函數(shù)用法實例詳解_python
- 2022-10-22 C#中的屬性解析(get、set、value)_C#教程
- 2022-07-14 Android?Studio使用自定義對話框效果_Android
- 2022-09-15 C++中的整形字節(jié)數(shù)_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支