-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdllist.py
70 lines (64 loc) · 1.96 KB
/
dllist.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
class Node:
def __init__(self, data):
self.item = data
self.nref = None
self.pref = None
class DoublyLinkedList:
def __init__(self, data_list=None):
if data_list is None:
self.start_node = None
else:
prev_node = None
for node in data_list:
if prev_node is None:
self.start_node = node
else:
prev_node.nref = node
node.pref = prev_node
prev_node = node
def insert(self, data):
if self.start_node is None:
self.start_node = data
self.start_node.pref = None
self.start_node.nref = None
else:
data.nref = self.start_node
data.pref = None
self.start_node.pref = data
self.start_node = data
def insert_after(self, data, prev):
if prev is None:
data.pref = None
data.nref = self.start_node
if self.start_node is not None:
self.start_node.pref = data
self.start_node = data
else:
data.pref = prev
data.nref = prev.nref
prev.nref = data
if data.nref is not None:
data.nref.pref = data
def delete(self, data):
p = data.pref
if p != None:
p = p.item
q = data.nref
if q != None:
q = q.item
if self.start_node is data:
self.start_node = data.nref
if self.start_node != None:
self.start_node.pref = None
elif data.nref is None:
data.pref.nref = None
else:
data.pref.nref = data.nref
data.nref.pref = data.pref
def to_list(self):
result = []
node = self.start_node
while node is not None:
result.append(node.item)
node = node.nref
return result