数据结构库——顺序队列的实现

1,队列本质:

       1,队列是一种特殊的线性表(对比前面的栈);

       2,队列仅能在线性表的两端进行操作(生活中的排队):

              1,队头(Font):取出数据元素的一端;

              2,队尾(Rear):插入数据元素的一端;

             

2,队列特性(唯一特性):

  

       1,先进先出(First In First Out);

      

3,队列的操作(创销进出获长清):

       1,创建队列(Queue()):

       2,销毁队列(~Queue());

       3,进队列(add());

       4,出队列(remove());

       5,获取队列头元素(front())(先来先服务,只提供队头获取方式);

       6,获取队列的长度(length());

       7,清空队列(clear());

      

4,队列的实现:

  

      

5,队列的顺序实现:

  

      

6,StaticQueue 设计要点:

       1,类模板:

              1,使用原生数组作为队列的存储空间;

              2,使用模板参数决定队列的最大容量;

                

7,StaticQueue 实现要点(循环计数法,为了高效):

       1,关键操作:

              1,进队列:m_space[m_rear] = e; m_rear = (m_rear + 1)%N;

              2,出队列:m_front = (m_front + 1)% N;

       2,队列的状态:

              1,队空:(m_length == 0) && (m_front == m_rear);

              2,队满:(m_length == N) && (m_front == m_rear);

8,基于顺序存储结构的队列 StaticQueue 实现:

 1 #ifndef STATICQUEUE_H
 2 #define STATICQUEUE_H
 3 
 4 #include "Queue.h"
 5 #include "Exception.h"
 6 
 7 namespace DTLib
 8 {
 9 
10 template < typename T, int N >
11 class StaticQueue : public Queue<T>
12 {
13 protected:
14     T m_space[N];  // 队列存储空间;
15     int m_front;  // 队头标识变量;
16     int m_rear;  // 队尾标识变量;
17    int m_length;  // 队列长度
18 
19 public:
20     StaticQueue()
21     {
22         m_front = 0;// 由于队列的为空的条件可知,这里设置为 0 也可以表示队列为空;
23         m_rear = 0;
24         m_length = 0;
25    }
26 
27     int capacity() const
28     {
29         return N;
30    }
31 
32     void add(const T& e)    // O(1)
33     {
34         if( m_length < N )
35         {
36             m_space[m_rear] = e;   // 行为时刻作用于类的属性
37             m_rear = (m_rear + 1) % N;   // 循环计数法,可以不用纠结于在那个位置进队列,也就是不用移动元素,很是提高效率;
38             m_length++;
39         }
40         else
41         {
42             THROW_EXCEPTION(InvalidOperationException, "No space in current StaticQueue ...");
43         }
44    }
45 
46     void remove()  // O(1)
47     {
48         if( m_length > 0 )
49         {
50             m_front = (m_front + 1) % N;  // 循环计数法,可以不用纠结于在哪//里出队列,也不用移动元素,很是高效;
51             m_length--;
52         }
53         else
54         {
55             THROW_EXCEPTION(InvalidOperationException, "No element in current queue ...");
56         }
57    }
58 
59     T front() const   // O(1)
60     {
61         if( m_length > 0 )
62         {
63             return m_space[m_front];   // 直接取,不对队列做操作
64         }
65         else
66         {
67             THROW_EXCEPTION(InvalidOperationException, "No element in current queue ...");
68         }
69    }
70 
71     int length() const    // O(1)
72     {
73         return m_length;
74    }
75 
76     void clear()   // O(1)
77     {
78         m_front = 0;
79         m_rear = 0;
80         m_length = 0;
81    }
82 
83     ~StaticQueue()
84     {
85         clear();
86     }
87 };
88 
89 }
90 
91 #endif // STATICQUEUE_H

9,StaticQueue 检测代码:

 1 #include <iostream>
 2 #include "StaticQueue.h"
 3 
 4 using namespace std;
 5 using namespace DTLib;
 6 
 7 int main()
 8 {
 9    StaticQueue<int, 5> queue;
10 
11     for(int i=0; i<5; i++)
12     {
13         queue.add(i);
14    }
15 
16     while( queue.length() > 0 )
17     {
18         cout << queue.front() << endl;
19         queue.remove();
20    }
21 
22     return 0;
23

             

10,小结:

       1,队列是一种特殊的线性表,具有先进先出的特性;

       2,队列只允许在线性表的两端进行操作,一端进,一端出;

       3,StaticQueue 使用原生数组作为内部存储空间;

       4,StaticQueue 的最大容量由模板参数决定;

              1,StaticQueue 是一种容器;

       5,StaticQueue 采用循环计数法提高队列操作的效率;

原文地址:https://www.cnblogs.com/dishengAndziyu/p/10923083.html