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中进行测试