-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDesignCircularQueue.py
47 lines (33 loc) · 1007 Bytes
/
DesignCircularQueue.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
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 en_queue(self, value: int) -> bool:
if self.is_full():
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 de_queue(self) -> bool:
if self.is_empty():
return False
self.head = self.head.next
self.size -= 1
return True
def front(self) -> int:
return -1 if self.is_empty() else self.head.val
def rear(self) -> int:
return -1 if self.is_empty() else self.tail.val
def is_empty(self) -> bool:
return self.size == 0
def is_full(self) -> bool:
return self.capacity == self.size