-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDesignCircularLinkedlist.py
56 lines (40 loc) · 1.39 KB
/
DesignCircularLinkedlist.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
55
56
class ListNode:
def __init__(self, val, nxt, prev):
self.val, self.next, self.prev = val, nxt, prev
class MyCircularQueue:
def __init__(self, k: int):
self.space = k
self.left = ListNode(0, None, None)
self.right = ListNode(0, None, self.left)
self.left.next = self.right
def enQueue(self, value: int) -> bool:
if self.isFull(): return False
curr = ListNode(value, self.right, self.right.prev)
self.right.prev.next = curr
self.right.prev = curr
self.space -= 1
return True
def deQueue(self) -> bool:
if self.isEmpty(): return False
self.left.next = self.left.next.next
self.left.next.prev = self.left
self.space += 1
return True
def Front(self) -> int:
if self.isEmpty(): return -1
return self.left.next.val
def Rear(self) -> int:
if self.isEmpty(): return -1
return self.right.prev.val
def isEmpty(self) -> bool:
return self.left.next == self.right
def isFull(self) -> bool:
return self.space == 0
# Your MyCircularQueue object will be instantiated and called as such:
# obj = MyCircularQueue(k)
# param_1 = obj.enQueue(value)
# param_2 = obj.deQueue()
# param_3 = obj.Front()
# param_4 = obj.Rear()
# param_5 = obj.isEmpty()
# param_6 = obj.isFull()