-
Notifications
You must be signed in to change notification settings - Fork 21
/
ArrayQueue.h
59 lines (48 loc) · 1.01 KB
/
ArrayQueue.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
//
// Created by light on 19-11-7.
//
#ifndef ALG_ARRAYQUEUE_H
#define ALG_ARRAYQUEUE_H
#include "../array/array.h"
#include <cstring>
// 基于动态数组的单向队列
template<typename T>
class ArrayQueue : public BaseQueue<T> {
private:
Array<T> *array;
public:
ArrayQueue(int capacity) {
array = new Array<T>(capacity);
}
ArrayQueue() {
array = new Array<T>();
}
~ArrayQueue() {
delete []array;
}
int getSize() {
return array->getSize();
}
bool isEmpty() {
return array->isEmpty();
}
T getCapacity() {
return array->getCapacity();
}
// 入队: 尾部 O(1)
void enqueue(T e) {
array->addLast(e);
}
// 出队:头部 O(n)
T dequeue() {
return array->removeFirst();
}
T getFront() {
return array->getFirst();
}
friend ostream &operator<<(ostream &out, ArrayQueue &queue) {
out << *(queue.array);
return out;
}
};
#endif //ALG_ARRAYQUEUE_H