-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircularequeue.cpp
62 lines (57 loc) · 1.13 KB
/
circularequeue.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
class MyCircularQueue {
private:
int * q;
int size;
int front;
int rear;
public:
MyCircularQueue(int k) {
size = k;
q = new int [size];
front = rear = -1;
}
~MyCircularQueue()
{
delete [] q;
}
bool enQueue(int value) {
if (isFull())
return false;
else
{
if (isEmpty())
front = 0;
rear = (rear+1)%size;
q [rear] = value;
return true;
}
}
bool deQueue() {
if (isEmpty())
return false;
else
{
if (rear == front)
rear = front = -1;
else
front = (front+1)%size;
return true;
}
}
int Front() {
if (isEmpty())
return -1;
return q[front];
}
int Rear() {
if (isEmpty())
return -1;
return q[rear];
}
bool isEmpty() {
return (front == -1);
}
bool isFull() {
return (front == (rear+1)%size);
}
};