forked from msdohehrty/dsa555-s16
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.h
59 lines (56 loc) · 781 Bytes
/
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
template <typename T>
class Queue{
T* data_;
int max_;
int size_;
int front_;
int back_;
void grow(){
T* newdata=new T[max_+100];
int j=front_;
for(int i=0;i<size_;i++){
newdata[i]=data_[j];
j=(j+1)%max_;
}
max_=max_+100;
front_=0;
back_=size_;
delete [] data_;
data_=newdata;
}
public:
Queue(){
data_=new T[100];
max_=100;
size_=0;
front_=0;
back_=0;
}
void enqueue(const T& data){
if(size_ >=max_){
grow();
}
data_[back_]=data;
back_=(back_==max_-1? 0:back_+1);
size_++;
}
void dequeue(){
if(!isEmpty()){
front_=(front_+1)%max_;
size_--;
}
}
T front() const{
T rc;
if(!isEmpty()){
rc=data_[front_];
}
return rc;
}
bool isEmpty() const{
return size_==0;
}
~Queue(){
delete [] data_;
}
}