標籤:隊列 鏈式隊列
關於鏈式隊列
鏈式隊列又稱為鏈隊,是使用單鏈表實現的,需要一個頭指標一個尾指標
結構圖:
鏈隊需要的元素組成
/*鏈式隊列的每一個節點*/struct node{ int data;//儲存資料 struct node *next;//指向下一個節點的指標};
/*鏈式隊列*/typedef struct{ struct node *head;//頭指標 struct node *tail;//尾指標}LinkedQueue;
建立一個帶前端節點的空隊列
- 建立一個節點p
- 將p節點的next指向null
- 建立一個隊列
- 將隊列的head指標指向節點p
- 將隊列的tail指標指向節點p
建立出空節點的結構圖(實際建立出來沒有p這個指標)
/*初始化一個空的鏈式隊列*/LinkedQueue *initLinkedQueue(){ LinkedQueue *linkedQueue = (LinkedQueue *)malloc(sizeof(LinkedQueue));//建立隊列 struct node *p = (struct node *)malloc(sizeof(struct node));//建立第一個空節點 p->next = NULL;//讓第一個節點的next指向null linkedQueue->head = linkedQueue->tail = p;//將隊列的head和tail指標指向第一個節點 return linkedQueue;}
入隊操作
對於鏈式隊列不必考慮隊列滿不滿,因為永遠不會滿
1. 建立一個新的節點q
2. 將傳入的資料賦值給q的data
3. q的next指標指向null
4. 將隊列的tail指標指向的節點的next指標指向q節點
5. 隊列的tail指標指向新插入的那個q節點,tail永遠指向最後一個節點
/*入隊操作*/void insert(LinkedQueue *lq, int num){ struct node *q = (struct node *)malloc(sizeof(struct node));//在記憶體中申請一個新的節點 q->data = num;//給新節點傳入資料 q->next = NULL;//新節點的next指標為空白 lq->tail->next = q;//將尾節點所指的那個節點的下一個節點指向新節點 lq->tail = q;//尾指標指向新插入的節點}
出隊操作
- 聲明一個節點q
- 如果隊列不為空白,讓q指向前端節點後面的那個節點,也就是要刪除的那個節點
- 讓隊列的前端節點的next指向q所指節點 的後面的那個節點
- 將q所指節點的data儲存起來
- 銷毀q所指的那個節點(free(q))
- 注意如果只有最後一個節點出隊了,tail指標所指的節點記憶體已被回收,所以需要對tail指標做處理,讓隊列的head指標=tail指標
/*刪除隊列元素*/int delete(LinkedQueue *lq, int *temp){ struct node *q; if (isEmpty(lq)) { printf("隊列為空白!\n"); return 0; } else { q = lq->head->next;//讓q指向前端節點後面的那個節點 lq->head->next = q->next;//讓隊列的前端節點的next指向q所指節點 的後面的那個節點 *temp = q->data;//將q所指節點的data儲存起來 free(q);// 銷毀q所指的那個節點,回收記憶體 //當只有一個元素的時候,出隊後隊列為空白,此時要修改尾指標 if (lq->head->next == NULL) { lq->head = lq->tail; } return 1; }}
使用C語言實現鏈式隊列
#include<stdio.h>#include<stdlib.h>/*鏈式隊列的每一個節點*/struct node{ int data; struct node *next;};/*鏈式隊列*/typedef struct{ struct node *head; struct node *tail;}LinkedQueue;/*初始化一個空的鏈式隊列*/LinkedQueue *initLinkedQueue(){ LinkedQueue *linkedQueue = (LinkedQueue *)malloc(sizeof(LinkedQueue)); struct node *p = (struct node *)malloc(sizeof(struct node)); p->next = NULL; linkedQueue->head = linkedQueue->tail = p; return linkedQueue;}/*入隊操作*/void insert(LinkedQueue *lq, int num){ struct node *q = (struct node *)malloc(sizeof(struct node));//在記憶體中申請一個新的節點 q->data = num;//給新節點傳入資料 q->next = NULL;//新節點的next指標為空白 lq->tail->next = q;//將尾節點所指的那個節點的下一個節點指向新節點 lq->tail = q;//尾指標指向新插入的節點}/*判斷隊列是否為空白*/int isEmpty(LinkedQueue *lq){ if (lq->head == lq->tail){//如果頭尾指標指向一個,隊列就是為空白的 return 1; } else { return 0; }}/*刪除隊列元素*/int delete(LinkedQueue *lq, int *temp){ struct node *q; if (isEmpty(lq)) { printf("隊列為空白!\n"); return 0; } else { q = lq->head->next; lq->head->next = q->next; *temp = q->data; free(q); //當只有一個元素的時候,出隊後隊列為空白,此時要修改尾指標 if (lq->head->next == NULL) { lq->head = lq->tail; } return 1; }}/*遍曆隊列*/void display(LinkedQueue *lq){ struct node *q = lq->head->next; while (q != NULL) { printf("隊列元素為%d\n", q->data); q = q->next; }}int main(){ LinkedQueue *lq = initLinkedQueue(); insert(lq, 1); insert(lq, 2); insert(lq, 3); insert(lq, 4); insert(lq, 5); int temp = 0; delete(lq, &temp); printf("%d\n", temp); display(lq); return 0;}
【資料結構-隊列】鏈式隊列