-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircularDeque.py
More file actions
65 lines (56 loc) · 1.55 KB
/
Copy pathcircularDeque.py
File metadata and controls
65 lines (56 loc) · 1.55 KB
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
57
58
59
60
61
62
63
64
65
class Node:
def __init__(self, val, p=None, n=None):
self.val = val
self.prev = p
self.next = n
class MyCircularDeque:
def __init__(self, k: int):
self.k = k
self.l = 0
self.head = self.tail = None
def insertFront(self, value: int) -> bool:
if(self.l == self.k):
return 0
if(self.l == 0):
self.head = self.tail = Node(value)
else:
curr = Node(value)
self.head.prev, curr.next = curr, self.head
self.head = curr
self.l += 1
return 1
def insertLast(self, value: int) -> bool:
if(self.l == self.k):
return 0
if(self.l == 0):
self.head = self.tail = Node(value)
else:
curr = Node(value)
self.tail.next, curr.prev = curr, self.tail
self.tail = curr
self.l += 1
return 1
def deleteFront(self) -> bool:
if(self.l == 0):
return 0
self.head = self.head.next
self.l -= 1
return 1
def deleteLast(self) -> bool:
if(self.l == 0):
return 0
self.tail = self.tail.prev
self.l -= 1
return 1
def getFront(self) -> int:
if(self.l == 0):
return -1
return self.head.val
def getRear(self) -> int:
if(self.l == 0):
return -1
return self.tail.val
def isEmpty(self) -> bool:
return self.l == 0
def isFull(self) -> bool:
return self.l == self.k