學習就像一段長跑,比的不是誰跑得快,而是誰更能堅持!!
?1 隊列的概念及結構
隊列:只允許在一端進行插入數據操作,在另一端進行刪除數據操作的特殊線性表,隊列具有先進先出 FIFO(First In First Out)入隊列:進行插入操作的一端稱為隊尾。出隊列:進行刪除操作的一端稱為隊頭。
?和棧不同的是,隊列的出隊順序是唯一的!
2 隊列的實現
分析
有兩種實現隊列的方式:數組和鏈表。鏈表可以用單鏈表也可以用雙鏈表。
使用鏈表的結構實現更優一些,因為如果使用數組的結構,出隊列在數組頭上出數據,效率很低!
數組實現:效率低!!

鏈表實現:單鏈表更合適!!
思考一個問題,需要帶哨兵位的頭節點嗎?
- 其實都可以,不帶也可以,可以不用判斷直接尾插,但是如果帶了哨兵位的頭節點,要malloc,最后也要free釋放空間。
因為隊列我們只需要入隊(尾插)和出隊(頭刪),單鏈表都可以實現,不需要使用雙鏈表。但是我們要想,我們要怎么分清隊頭和隊尾呢?所以我們在尾插頭刪的時候:
- 需要ptail指針維護隊列最后一個元素
- 需要phead指針維護隊列第一個元素
那么這個時候實現起來就需要用到二級指針了。很不方便。
那么我們怎么解決這個問題呢?(不用二級指針的等效替換方法)
①帶哨兵位的頭節點。②返回值。③可以考慮用一個結構體封裝起來。
這里我們用結構體。?
代碼實現
Test.c
#define _CRT_SECURE_NO_WARNINGS 1#include"Queue.h"int main()
{Queue q;QueueInit(&q);QueuePush(&q, 1);QueuePush(&q, 2);QueuePush(&q, 3);printf("%d ", QueueFront(&q));QueuePop(&q);printf("%d ", QueueFront(&q));QueuePop(&q);QueuePush(&q, 4);QueuePush(&q, 5);while (!QueueEmpty(&q)){printf("%d ", QueueFront(&q));QueuePop(&q);}QueueDestroy(&q);return 0;
}
函數聲明Queue.h
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>typedef int QDataType;//創建隊列節點
typedef struct QueueNode
{QDataType val;struct QueueNode* next;
}QNode;//創建維護隊列的指針
typedef struct Queue
{QNode* phead;QNode* ptail;int size;//原本是需要遍歷的,寫在結構體里可以很好的是時間復雜度由O(N)變為O(1)
}Queue;//初始化
void QueueInit(Queue* pq);//空間釋放
void QueueDestroy(Queue* pq);//尾插
void QueuePush(Queue* pq, QDataType x);//頭刪
void QueuePop(Queue* pq);//取隊頭的數據
QDataType QueueFront(Queue* pq);//取隊尾的數據
QDataType QueueBack(Queue* pq);//判斷是否為空
bool QueueEmpty(Queue* pq);//隊列元素個數
int QueueSize(Queue* pq);
函數實現Queue.c?
初始化QueueInit
void QueueInit(Queue* pq)
{assert(pq);pq->phead = pq->ptail = NULL;pq->size = 0;
}
空間釋放QueueDestroy
void QueueDestroy(Queue* pq)
{assert(pq);QNode* cur = pq->phead;while (cur){QNode* next = cur->next;free(cur);cur = next;}pq->phead = pq->ptail = NULL;pq->size = 0;
}
入隊列QueuePush
這里需要創建一個節點
void QueuePush(Queue* pq, QDataType x)
{assert(pq);QNode* newnode = (QNode*)malloc(sizeof(QNode));if (newnode == NULL){perror("malloc fail");return;}newnode->val = x;newnode->next = NULL;if (pq->ptail == NULL){pq->ptail = pq->phead = newnode;}else{pq->ptail->next = newnode;pq->ptail = newnode;}pq->size++;
}
出隊列QueuePop
要注意兩種情況
- 空鏈表
- 只有一個元素(ptail野指針的情況,要進行判斷置空)
void QueuePop(Queue* pq)
{assert(pq);//鏈表不為空assert(pq->phead);QNode* del = pq->phead;pq->phead = pq->phead->next;free(del);del = NULL;//鏈表中只有一個元素,刪完以后為空if (pq->phead == NULL)pq->ptail = NULL;pq->size--;
}
隊頭元素QueueFront
QDataType QueueFront(Queue* pq)
{assert(pq);assert(pq->phead);return pq->phead->val;
}
隊尾元素QueueBack
QDataType QueueBack(Queue* pq)
{assert(pq); assert(pq->ptail);return pq->ptail->val;
}
判斷隊列是否為空QueueEmpty
bool QueueEmpty(Queue* pq)
{assert(pq);return pq->phead == NULL;
}
隊列元素個數QueueSize
int QueueSize(Queue* pq)
{assert(pq);return pq->size;
}
Queue.c總代碼
#define _CRT_SECURE_NO_WARNINGS 1#include"Queue.h"void QueueInit(Queue* pq)
{assert(pq);pq->phead = pq->ptail = NULL;pq->size = 0;
}void QueueDestroy(Queue* pq)
{assert(pq);QNode* cur = pq->phead;while (cur){QNode* next = cur->next;free(cur);cur = next;}pq->phead = pq->ptail = NULL;pq->size = 0;
}void QueuePush(Queue* pq, QDataType x)
{assert(pq);QNode* newnode = (QNode*)malloc(sizeof(QNode));if (newnode == NULL){perror("malloc fail");return;}newnode->val = x;newnode->next = NULL;if (pq->ptail == NULL){pq->ptail = pq->phead = newnode;}else{pq->ptail->next = newnode;pq->ptail = newnode;}pq->size++;
}void QueuePop(Queue* pq)
{assert(pq);// assert(pq->phead);QNode* del = pq->phead;pq->phead = pq->phead->next;free(del);del = NULL;if (pq->phead == NULL)pq->ptail = NULL;pq->size--;
}QDataType QueueFront(Queue* pq)
{assert(pq);// assert(pq->phead);return pq->phead->val;
}QDataType QueueBack(Queue* pq)
{assert(pq); assert(pq->ptail);return pq->ptail->val;
}bool QueueEmpty(Queue* pq)
{assert(pq);return pq->phead == NULL;
}int QueueSize(Queue* pq)
{assert(pq);return pq->size;
}
以上就是用單鏈表實現隊列的代碼實現。