-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircle_queue.h
executable file
·76 lines (66 loc) · 1.25 KB
/
circle_queue.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
/*
* circle_queue.h
*
* Created on: 2011-11-11
* Author: kevinguo
*/
#ifndef CIRCLE_QUEUE_H_
#define CIRCLE_QUEUE_H_
#include <stdlib.h>
template <typename T>
class CircleQueue {
public:
CircleQueue()
: m_data_array(NULL)
, size(0)
, capactity(0)
, head(0)
, tail(-1) {}
virtual ~CircleQueue() {}
bool Init(int _size) {
assert(_size > 0);
capactity = _size;
m_data_array = new T[_size];
assert(m_data_array != NULL);
memset(m_data_array, 0, sizeof(T)*size);
size = 0;
head = 0;
tail = -1;
return false;
}
bool UnInit() {
if (NULL == m_data_array) {
return false;
}
delete [] m_data_array;
}
bool Push(const T &data)
{
if (size >= capactity) {
return false;
}
tail = (tail + 1)%capactity;
m_data_array[tail] = data;
size += 1;
return true;
}
bool Pop(T &data) {
if (size == 0) {
return false;
}
data = m_data_array[head];
head = (head + 1)%capactity;
size--;
return true;
}
int GetSize() const{
return size;
}
private:
T *m_data_array;
int size;
int capactity;
int head;
int tail;
};
#endif /* CIRCLE_QUEUE_H_ */