-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.cpp
101 lines (89 loc) · 1.9 KB
/
Queue.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
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
#include <bits/stdc++.h>
using namespace std;
template <class T>
class Queue
{
private:
T *data;
int front,tail,capacity;
void resize(int newcapacity){
T *newdada = new T[newcapacity + 1];
for (int i = 0; i < getSize(); i++){
newdada[i] = data[(i + front) % capacity];
}
data = newdada;
tail = getSize();
front = 0;
capacity = newcapacity;
}
public:
Queue(int capacity){
data = new T[capacity + 1];
front = 0;
tail = 0;
this -> capacity = capacity;
};
Queue(){
data = new T[10];
front = 0;
tail = 0;
capacity = 10;
};
~Queue();
int getCapacity(){
return capacity;
}
bool empty(){
return front == tail;
}
int getSize(){
return (tail + capacity - front) % capacity;
}
void push(T e){
if((tail + 1) % capacity == front){
resize(capacity * 2);
}
data[tail] = e;
tail = (tail + 1) % capacity;
}
void pop(){
assert(front != tail);
front = (front + 1) % capacity;
if(getSize() == capacity / 4 && capacity / 2 != 0)
resize(capacity / 2);
}
int Front(){
assert(front != tail);
return data[front];
}
int back(){
assert(front != tail);
return data[tail];
}
void print(){
cout << "Queue: size = " << getSize() << ", capacity = " << capacity << std::endl;
cout << "front [";
for (int i = front; i != tail; i = (i + 1) % capacity) {
cout << data[i];
if ((i + 1) % capacity != tail) {
cout << ", ";
}
}
cout << "] tail" << endl;
}
};
int main(int argc, char const *argv[])
{
Queue<int> *queue = new Queue<int>(5);
for (int j = 0; j < 5; ++j) {
queue -> push(j);
queue -> print();
}
queue -> push(20);
queue -> push(33);
queue -> print();
queue -> pop();
queue -> print();
cout << queue -> Front() << endl;
return 0;
}