forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0622-design-circular-queue.py
54 lines (33 loc) · 1006 Bytes
/
0622-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
53
class Node:
def __init__(self, val: int):
self.val = val
self.next = None
class MyCircularQueue:
def __init__(self, k: int):
self.head = self.tail = None
self.capacity = k
self.size = 0
def enQueue(self, value: int) -> bool:
if self.isFull():
return False
node = Node(value)
if self.size == 0:
self.head = self.tail = node
else:
self.tail.next = self.tail = node
self.size += 1
return True
def deQueue(self) -> bool:
if self.isEmpty():
return False
self.head = self.head.next
self.size -= 1
return True
def Front(self) -> int:
return -1 if self.isEmpty() else self.head.val
def Rear(self) -> int:
return -1 if self.isEmpty() else self.tail.val
def isEmpty(self) -> bool:
return self.size == 0
def isFull(self) -> bool:
return self.capacity == self.size