queue (C++中STL庫常用queue基本用法的實現) ([連結串列],[陣列]的實現)
阿新 • • 發佈:2018-12-16
Queue: 依循先進先出的規則的單調佇列.
下面是用連結串列實現的queue的幾個基本用法和一個clear()的補充用法:
#include<stdio.h> /* *Date:2018/10/22 *Author:Fushicho *Name:queue連結串列版 *Function:push(),pop(),front(),empty(),size(),back(),clear() */ struct queue { private: struct node { int num; //節點資料 node *next_p; //節點指標指向該節點的下一個節點 node() { //建構函式(初始化) this->next_p = NULL; } }; node *head; //佇列頭指標 node *tail; //佇列尾指標 public: queue() { //建構函式(初始化) head = NULL; tail = NULL; } void push(int x) { //在佇列尾加入一個元素 node *p = new node(); p->num = x; if (empty()) { //佇列為空 head = p; tail = p; } else { //佇列不為空 tail->next_p = p; tail = p; } } void pop() { //將佇列頭節點彈出並釋放記憶體 node *p = head; head = head->next_p; delete(p); } int front() { //獲得佇列首元素 return head->num; } int back() { //輸出隊尾元素 return tail->num; } bool empty() { //判斷佇列是否為空 return head == NULL; } int size() { //計算佇列元素個數 if (empty())return 0; int cnt = 0; node *p = head; while (p != tail) { p = p->next_p; cnt++; } return cnt+1; } void clear() { //清空(需要把每個節點都釋放記憶體,優點:節省記憶體,缺點:時間慢) node *p; while (head != tail) { p = head; head = head->next_p; delete(p); } head = tail = NULL; } }; void debug() { //測試程式碼 queue que; int a[] = { 1,2,3,4,5,6 }; printf("入佇列順序:"); for (int i = 0; i < 6; i++) { printf("%d ", a[i]); que.push(a[i]); //push()測試 } printf("\n"); printf("佇列元素個數:%d\n", que.size()); //size()測試 que.clear(); //clear()測試 for (int i = 0; i < 6; i++) que.push(a[i]), printf("隊尾元素為:%d\n", que.back());//back()測試 printf("出佇列順序:"); while (!que.empty()) { //empty()測試 int x = que.front(); que.pop(); //pop()測試 printf("%d ", x); } printf("\n"); } int main() { debug(); return 0; }
下面是用陣列實現的queue的幾個基本用法和一個clear()的補充用法:
#include<stdio.h> const int maxn = 10; //為了測試迴圈陣列是否實現成功取一個較小的值(測試完後可以根據需求該大小) /* *Date:2018/10/22 *Author:Fushicho *Name:queue陣列版 *Function:push(),pop(),front(),empty(),size(),back(),clear() */ struct queue { private: int que[maxn]; //佇列陣列(最大儲存個數:manx , 在多次使用下采用迴圈陣列方式實現重複利用空間) int head, tail; //這裡的"頭指標"和"尾指標"都是一個int,用作記錄頭和尾指標的下標 /*迴圈陣列:存放que的方式有2種: (1) [head,tail) (2) [head,maxn) and [0,tail) */ //且head和tail都小於maxn public: queue() { //建構函式(初始化) head = tail = 0; } void push(int x) { //在佇列尾加入一個元素 que[tail++] = x; tail %= maxn; } void pop() { //將佇列頭節點彈出並釋放記憶體 head++; head %= maxn; } int front() { //獲得佇列首元素 return que[head]; } int back() { //獲得佇列尾元素 return que[((tail - 1)%maxn + maxn) % maxn]; //(避免tail出現負數) } bool empty() { //判斷佇列是否為空 return head == tail; } int size() { //獲得佇列元素個數 if (head <= tail) return tail - head; return maxn - head + tail; } void clear() { //清空(只需要修改頭指標和尾指標,優點:節省時間) head = tail; } }; void debug() { //測試程式碼 queue que; int a[] = { 1,2,3,4,5,6 }; printf("入佇列順序:"); for (int i = 0; i < 6; i++) { printf("%d ", a[i]); que.push(a[i]); //push()測試 } printf("\n"); printf("佇列元素個數:%d\n", que.size()); //size()測試 que.clear(); //clear()測試 for (int i = 0; i < 6; i++) que.push(a[i]), printf("隊尾元素為:%d\n", que.back()); //back()測試 printf("出佇列順序:"); while (!que.empty()) { //empty()測試 int x = que.front(); que.pop(); //pop()測試 printf("%d ", x); } printf("\n"); } int main() { debug(); return 0; }
若有不懂或者有寫錯的地方請在下方評論指出,謝謝~