队列是常用的数据结构之一,可以采用表直接很容易实现,为了弄清原理,我们采用数组实现
1 /************************************************************************/
2 /* 队列的实现,可以通过表直接实现,这里采用数组实现
3 /************************************************************************/
4
5 #ifndef QUEUE_H
6
7 #define QUEUE_H
8
9 #include <assert.h>
10
11 namespace stl
12 {
13 template <typename Object>
14 class Queue
15 {
16 Queue(int QueueSize)
17 :Front(0)
18 ,Rear(1)
19 ,Size(0)
20 ,Capacity(QueueSize)
21 {
22 Array = new Object[QueueSize];
23 }
24
25 ~Queue()
26 {
27 assert(Array);
28 delete[] Array;
29 }
30
31 //入队函数
32 void EnQueue(Object x)
33 {
34 if (IsFull())
35 {
36 throw();
37 }
38 Array[Rear++] = x;
39 Rear = (++Rear) % Capacity;
40 ++Size;
41 }
42
43 //出队函数
44 void DeQueue()
45 {
46 if (IsEmpty())
47 {
48 throw();
49 }
50 ++Front;
51 --Size;
52 }
53
54 bool IsFull()
55 {
56 return Size == Capacity;
57 }
58
59 bool IsEmpty()
60 {
61 return Size == 0;
62 }
63
64 private:
65 Object* Array;
66 int Capacity; //队列最大容量
67 int Front; //队列头位置
68 int Rear; //队列尾的位置
69 int Size; //队列中对象的数量
70 };
71 }
72 #endif // !QUEUE_H
原文链接: https://www.cnblogs.com/liuteng/p/6415001.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/249576
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!