-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path622. Design Circular Queue.py
52 lines (42 loc) · 1.01 KB
/
622. Design Circular Queue.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
class MyCircularQueue:
def __init__(self, k: int):
self.maxSize = k
self.queue = [0] * k
self.front = 0
self.rear = -1
def enQueue(self, value: int) -> bool:
if self.isFull():
return False
self.queue[self.rear] = value
self.rear = (self.rear + 1) % self.maxSize
return True
def deQueue(self) -> bool:
if self.isEmpty():
return False
self.front = (self.front + 1) % self.maxSize
return True
def Front(self) -> int:
if self.isEmpty():
return -1
return self.queue[self.front]
def Rear(self) -> int:
if self.isEmpty():
return -1
return self.queue[self.rear - 1]
def isEmpty(self) -> bool:
return self.rear - self.front == -1
def isFull(self) -> bool:
size = self.rear - self.front + 1
if self.rear < self.front and self.rear > -1:
size += self.maxSize
return size == self.maxSize
q = MyCircularQueue(3)
print(q.enQueue(1))
print(q.enQueue(2))
print(q.enQueue(3))
print(q.enQueue(4))
print(q.Rear())
print(q.isFull())
print(q.deQueue())
print(q.enQueue(4))
print(q.Rear())