-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlinked_list.py
131 lines (103 loc) · 3 KB
/
linked_list.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
class Linked_List_Element:
"""
Linked-List element.
"""
def __init__(self, key, value):
"""
Generates a linked list element with the given key
and value.
"""
self.key = key
self.value = value
self.prev = None
self.next = None
def find(self, key):
"""
Returns an element holding key in self's sub-list.
If no such element exists, then None is returned.
"""
target = self
while target and target.key != key:
target = target.next
return target
def insert(self, element):
"""
Inserts element at the beginning of self's
linked-list.
"""
self.prev = element
element.next = self
return element
def update(self, value):
"""
Sets self's value.
"""
self.value = value
return self
def delete(self):
"""
Deletes self from the linked-list.
"""
if self.prev:
self.prev.next = self.next
if self.next:
self.next.prev = self.prev
return self
def __repr__(self):
"""
Returns the string representation of self.
"""
return "(k: " + str(self.key) + ", v: " + str(self.value) + ")"
class Linked_List:
"""
Linked-List data structure.
"""
def __init__(self):
"""
Generates an empty linked-list.
"""
self.head = None
def find(self, key):
"""
Returns the element in the linked-list holding key
if such element exists. Returns None otherwise.
"""
return self.head and self.head.find(key)
def insert(self, key, value):
"""
Inserts an element holding key and value into the
linked-list.
"""
element = Linked_List_Element(key, value)
if self.head:
self.head.insert(element)
self.head = element
return element
def delete(self, key):
"""
Deletes an element holding key k form the linked list.
"""
target = self.find(key)
if target:
if target is self.head:
self.head = target.next
target.delete()
return target
def update(self, key, value):
"""
Finds an element holding key and updates its value.
If no such element exits, then inserts a new element
holding key and value.
"""
target = self.find(key)
return target.update(value) if target else self.insert(key,value)
def __repr__(self):
"""
Returns the string representation of a linked-list.
"""
rep = []
element = self.head
while element:
rep.append((element.key, element.value))
element = element.next
return str(rep)