-
Notifications
You must be signed in to change notification settings - Fork 11
/
CircularQ.py
54 lines (46 loc) · 1.37 KB
/
CircularQ.py
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
# Python implementation of Circular Q
class CircularQ():
def __init__(self, capacity):
self.queue = [None] * capacity
self.capacity = capacity
self.front = 0
self.rear = -1
self.size = 0
def enqueue(self, value):
if self.size == self.capacity:
raise OverflowError('Queue is full')
self.rear = (self.rear + 1) % self.capacity
self.queue[self.rear] = value
self.size += 1
def dequeue(self):
if self.isEmpty():
raise IndexError('Queue is empty')
value = self.queue[self.front]
self.front = (self.front + 1) % self.capacity
self.size -= 1
return value
def isEmpty(self):
return self.size == 0
def front(self):
if not self.isEmpty():
return self.queue[self.front]
raise IndexError('Queue is empty')
def rear(self):
if not self.isEmpty():
return self.queue[self.rear]
raise IndexError('Queue is empty')
# Test Cases
queue = CircularQ(3)
queue.enqueue(1)
queue.enqueue(2)
queue.enqueue(3)
assert queue.front() == 1
assert queue.rear() == 3
assert queue.dequeue() == 1
assert queue.front() == 2
assert queue.rear() == 3
assert queue.dequeue() == 2
assert queue.front() == 3
assert queue.rear() == 3
assert queue.dequeue() == 3
assert queue.isEmpty() == True