本文实例为大家分享了C++实现循环队列的具体代码,供大家参考,具体内容如下
circularQueue.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
#pragma once #pragma once #ifndef CIRCULARQUEUE_H #define CIRCULARQUEUE_H #include<iostream> #include<ostream> using std::cout; using std::cin; using std::endl; using std::ostream; template < class T> class cirQueue; template < typename T> class cirQueue { public : cirQueue( int sz); ~cirQueue(); void push( const T& elem); //进队 void pop(T& elem); //出队 bool empty(); //查看队列是否为空 int getSize(); //返回队列中元素的个数 void clearQueue(); //清空队列中的元素 void print(); //打印队列中的元素 int getfront() { return front; } int getrear() { return rear; } bool getTop(T& elem); //读取队列首个元素 template < typename T> friend ostream& operator<<(ostream& os, cirQueue<T>& queue); private : bool _full() const ; //判断队列是否已满 int maxsize; //队列最大的空间 T* element; //存放于队列中的元素数组 int front; //模拟队头指针 int rear; //模拟队尾指针 }; template < typename T> cirQueue<T>::cirQueue( int sz) { maxsize = sz; element = new T[maxsize]; if (element == nullptr) cout << "内存分配失败" << endl; front = 0; rear = 0; } template < typename T> cirQueue<T>::~cirQueue() { if (element != nullptr) delete element; } //进队 template < typename T> void cirQueue<T>::push( const T& elem) { //需要保证队尾指针位置与首个元素相差一个位置 if (rear > (maxsize - 1)) rear -= maxsize ; if (front > (maxsize - 1)) front -= maxsize ; if (!_full()) { //队列未满的情况 element[rear++] = elem; //队尾向后移动一位 //++rear; } else { cout << "队列已满,不能插入!" << endl; return ; } } //出队 template < typename T> void cirQueue<T>::pop(T& elem) { if (rear > (maxsize - 1)) rear -= (maxsize - 1); if (front > (maxsize - 1)) front -= (maxsize - 1); if (!empty()) { //队列未空的情况 elem = element[front++]; //队头向后移动一位 element[front - 1] = 0; //置零 } else { cout << "队列已空!" << endl; return ; } } //查看队列是否为空 template < typename T> bool cirQueue<T>::empty() { if (front == rear) //待定 return true ; return false ; } //返回队列中元素的个数 template < typename T> int cirQueue<T>::getSize() { int num = 0; if (front <= rear) return rear - front; else return maxsize - front + rear + 1; } //清空队列中的元素 template < typename T> void cirQueue<T>::clearQueue() { if (!empty()) { int Index = 0; while (front < rear) { //front逼近rear element[front++] = 0; if (front == rear) return ; } if (rear < front) { while (front <= maxsize - 1) //删除front至数组尾端的数据 element[front++] = 0; front -= maxsize; while (front < rear) { //删除front至rear的数据 element[front++] = 0; if (front == rear) return ; } } } } //打印队列中的元素 template < typename T> void cirQueue<T>::print() { //与clearQueue函数原理一致,将front替换为Index if (!empty()) { int Index = front; while (Index < rear) { cout << element[Index++] << " " ; if (Index == rear) { cout << endl; return ; } } if (rear < Index) { while (Index <= maxsize - 1) cout << element[Index++] << " " ; Index -= maxsize; while (Index < rear) { cout << element[Index++] << " " ; if (Index == rear) { cout << endl; return ; } } } } } //读取队列首个元素 template < typename T> bool cirQueue<T>::getTop(T& elem) { if (!empty()) { elem = element[front]; return true ; } return false ; } template < typename T> ostream& operator<<(ostream& os, cirQueue<T>& queue) { os << "队列中的元素数量为:" << queue.getSize() << endl; return os; } //判断队列是否已满 template < typename T> bool cirQueue<T>::_full() const { if (front - rear == 1 || front - rear == -maxsize + 1) return true ; return false ; } #endif // !CIRCULARQUEUE_H |
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
#include"CircularQueue.h" int main() { cirQueue< int > cq(20); int a = 0; for ( int i = 0; i < 19; i++) { cq.push(i); } cq.print(); cout << cq; for ( int i = 0; i < 20; i++) { cq.pop(a); } cout << cq; //此时front=rear=19 cout << cq.getfront() << " " << cq.getrear() << endl; //for (int i = 19; i < 25; i++) //{ // cq.push(i); //} cq.push(19); cq.print(); cout << cq.getfront() << " " << cq.getrear() << endl; cout << endl << endl; cq.push(20); cq.getTop(a); cout << a << endl; cq.print(); cout << cq.getfront() << " " << cq.getrear() << endl; return 1; } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/candand_python/article/details/103946835