-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCircularQueue.h
58 lines (47 loc) · 1016 Bytes
/
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
//
// Created by mengjie on 11/22/18.
//
#ifndef DEMO_CIRCULARQUEUE_H
#define DEMO_CIRCULARQUEUE_H
#define MAX_LENGTH 5
class CircularQueue{
private:
int *queue;
int rear,front;
public:
int length(){
return (rear-front+MAX_LENGTH) % MAX_LENGTH;
}
CircularQueue(){
queue = new int[MAX_LENGTH];
rear = 0;
front = 0;
}
bool isEmpty(){
return rear==front;
}
bool enqueue(int e){
if((rear+1)%MAX_LENGTH == front) return false;
else{
queue[rear] = e;
rear = (rear+1)%MAX_LENGTH;
return true;
}
}
bool dequeue(){
if(isEmpty()) return false;
else{
front = (front+1)%MAX_LENGTH;
return true;
}
}
bool dequeue(int &e){
if(isEmpty()) return false;
else{
e = queue[front];
front = (front+1)%MAX_LENGTH;
return true;
}
}
};
#endif //DEMO_CIRCULARQUEUE_H