Skip to content

Commit aae947d

Browse files
committed
feature: 0622-design-circular-queue.py
1 parent 7d8d654 commit aae947d

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

python/0622-design-circular-queue.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
class Node:
2+
def __init__(self, val: int):
3+
self.val = val
4+
self.next = None
5+
6+
7+
class MyCircularQueue:
8+
9+
def __init__(self, k: int):
10+
self.head = self.tail = None
11+
self.capacity = k
12+
self.size = 0
13+
14+
15+
def enQueue(self, value: int) -> bool:
16+
if self.isFull():
17+
return False
18+
19+
node = Node(value)
20+
if self.size == 0:
21+
self.head = self.tail = node
22+
else:
23+
self.tail.next = self.tail = node
24+
25+
self.size += 1
26+
27+
return True
28+
29+
30+
def deQueue(self) -> bool:
31+
if self.isEmpty():
32+
return False
33+
34+
self.head = self.head.next
35+
self.size -= 1
36+
37+
return True
38+
39+
40+
def Front(self) -> int:
41+
return -1 if self.isEmpty() else self.head.val
42+
43+
44+
def Rear(self) -> int:
45+
return -1 if self.isEmpty() else self.tail.val
46+
47+
48+
def isEmpty(self) -> bool:
49+
return self.size == 0
50+
51+
52+
def isFull(self) -> bool:
53+
return self.capacity == self.size
54+

0 commit comments

Comments
 (0)