-
Notifications
You must be signed in to change notification settings - Fork 0
/
deque.py
87 lines (75 loc) · 2.38 KB
/
deque.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
class Node:
def __init__(self, value, previos=None, forward=None):
self.previos = previos
self.forward = forward
self.value = value
class Deque:
def __init__(self):
self.tail = None
self.head = None
self.mid = None
self.len = 0
def push_back(self, val):
new_node = Node(val, None, self.tail)
if self.len < 1:
self.head = self.tail = self.mid = new_node
else:
self.tail.previos = new_node
self.tail = new_node
if self.len % 2 != 0:
self.mid = self.mid.previos
self.len += 1
def push_forward(self, val):
new_node = Node(val, self.head, None)
if self.len < 1:
self.tail = self.head = self.mid = new_node
else:
self.head.forward = new_node
self.head = new_node
if self.len % 2 == 0:
self.mid = self.mid.forward
self.len += 1
def push_mid(self, val):
new_node = Node(val, None, None)
if self.len < 1:
self.tail = self.head = self.mid = new_node
else:
forward = self.mid.forward
self.mid.forward = new_node
new_node.forward = forward
new_node.previos = self.mid
if forward:
forward.previos = new_node
self.mid = new_node
self.len += 1
def pop_back(self):
if self.len < 1:
return 'Error!'
elif self.len < 2:
val = self.tail.value
self.tail = self.head = self.mid = None
self.len -= 1
return val
else:
val = self.tail.value
self.tail = self.tail.forward
self.len -= 1
if self.len % 2 != 0:
self.mid = self.mid.forward
return val
def pop_front(self):
if self.len < 1:
return 'Error!'
elif self.len < 2:
val = self.head.value
self.tail = self.head = self.mid = None
self.len -= 1
return val
else:
val = self.head.value
self.head = self.head.previos
self.head.forward = None
self.len -= 1
if self.len % 2 == 0:
self.mid = self.mid.previos
return val