C++實現隊列

  • 2020 年 10 月 29 日
  • 筆記

固定容量的循環隊列

基礎方法:

  • 判空
  • 判滿
  • 進隊
  • 出隊

輔助方法:

  • 輸出隊列內元素個數
  • 按順序輸出隊列元素

sichqueue.h

 1 #pragma once
 2 #include<string>
 3 
 4 namespace sichdc {
 5 
 6     class SQueue {
 7 
 8     private:
 9 
10         std::string name;    // 對象名
11 
12         static const int cap = 5;    // 隊列容量
13         int queue[cap + 1];        // 隊列「容器」
14         int front;        // 頭指針
15         int rear;        // 尾指針
16 
17     public:
18 
19         SQueue(std::string n);
20 
21         bool IsEmpty();
22         bool IsFull();
23 
24         bool Push(int n);    // 進隊
25         bool Pop();            // 出隊
26 
27         int Size();        // 返回實際大小
28 
29         void Check();    // 輸出隊列中當前所有元素
30     };
31     class SQueueTest {
32 
33     public:
34 
35         void Test();
36     };
37 }

 

sichqueue.cpp

  1 #include"sichqueue.h"
  2 #include<iostream>
  3 
  4 namespace sichdc {
  5 
  6     SQueue::SQueue(std::string n) {
  7 
  8         name = n;    // 設置對象名
  9 
 10         for (int i = 0; i < cap; ++i) {
 11             // 將數組初始化
 12 
 13             queue[i] = 0;
 14         }
 15 
 16         front = 0;        // front!=rear時,front表示隊列中第一個元素位置
 17         rear = 0;        // rear指向當前隊列的可插入位置
 18     }
 19     bool SQueue::IsEmpty() { return front == rear; }
 20     bool SQueue::IsFull() { return (rear + 1)%(cap + 1)  == front; }    // 考慮到隊列的循環
 21     bool SQueue::Push(int n) {
 22 
 23         if (IsFull()) { return false; }
 24 
 25         queue[rear] = n;
 26         // 更新rear
 27         if (rear == cap) { rear = 0; }
 28         else { rear += 1; }
 29 
 30         return true;
 31     }
 32     bool SQueue::Pop() {
 33 
 34         if (IsEmpty()) { return false; }
 35 
 36         if (front == cap) { front = 0; }
 37         else { front += 1; }
 38 
 39         return true;
 40     }
 41     int SQueue::Size() {
 42 
 43         if (rear > front) { return rear - front; }
 44         else if (rear == front) { return 0; }
 45         else { return rear + cap + 1 - front; }
 46     }
 47     void SQueue::Check() {
 48 
 49         using namespace std;
 50 
 51         cout << "對象 " << name << " :";
 52         int i = front;
 53         while (i != rear) {
 54 
 55             cout << queue[i] << ", ";
 56             i = (i + 1) % (cap + 1);
 57         }
 58         cout << "\n" << endl;
 59     }
 60 
 61     void SQueueTest::Test() {
 62 
 63         using namespace std;
 64 
 65         SQueue sq{ "sq" };
 66 
 67         string cmd{};    // 用於接受用戶命令
 68         int n{};        // 用於接收用戶數據
 69 
 70         cout << "_> ";
 71         cin >> cmd;
 72         while (cmd != "退出") {
 73 
 74             if (cmd == "空隊列") {
 75 
 76                 cout << (sq.IsEmpty() ? "空隊列" : "非空隊列") << "\n" << endl;
 77             }
 78             else if (cmd == "滿隊列") {
 79 
 80                 cout << (sq.IsFull() ? "滿隊列" : "非滿隊列") << "\n" << endl;
 81             }
 82             else if (cmd == "隊列大小"){
 83 
 84                 cout << sq.Size() << "\n" << endl;
 85             }
 86             else if (cmd == "進隊") {
 87 
 88                 cout << "輸入數據:";
 89                 cin >> n;
 90                 cout << sq.Push(n) << "\n" << endl;
 91             }
 92             else if (cmd == "出隊") {
 93 
 94                 cout << sq.Pop() << "\n" << endl;
 95             }
 96             else if (cmd == "檢查") {
 97 
 98                 sq.Check();
 99             }
100             else {
101 
102                 cout << "不支援當前命令!" << "\n" << endl;
103             }
104 
105             cout << "_> ";
106             cin >> cmd;
107         }
108     }
109 }

 

源.cpp

 1 #include"sichqueue.h"
 2 #include<iostream>
 3 
 4 int main() {
 5 
 6     using namespace std;
 7     using sichdc::SQueueTest;
 8 
 9     cout << "## 程式開始 ##\n";
10 
11     SQueueTest sqt{};
12     sqt.Test();
13 
14     cout << "## 程式結束 ##\n";
15     return 0;
16 }

 

備註:

sichqueue.h文件定義了循環隊列的實現類和測試類

sichqueue.cpp實現這兩個類

在源.cpp中進行測試