-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_list_data_structure.py
206 lines (160 loc) · 5.3 KB
/
linked_list_data_structure.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
__author__ = 'ivishnevskiy'
class Node():
def __init__(self, data, next):
self.data = data
self.next = next
def getData(self):
return self.data
def setData(self, value):
self.data = value
def getNext(self):
return self.next
def setNext(self, newNext):
self.next = newNext
class SinglyLinkedList():
def __init__(self):
self.head = None
self.tail = None
self.currentSize = 0
def add(self, value):
if(self.isEmpty()):
self.head = Node(value, None)
self.tail = self.head
else:
self.tail.setNext(Node(value, None))
self.tail = self.tail.getNext()
self.currentSize += 1
return True
def isEmpty(self):
return self.size() == 0
def size(self):
return self.currentSize
def get(self, index):
if index < 0 or index >= self.currentSize:
raise IndexError
if index < self.currentSize - 1:
current = self.head
for i in xrange(index):
current = current.getNext()
return current.getData()
return self.tail.getData()
def clear(self):
self.head = None
self.tail = None
self.currentSize = 0
def removeByIndex(self, index):
if index < 0 or index >= self.currentSize:
raise IndexError
current = self.head
for i in xrange(0, self.size()-1):
last = current
current = current.getNext()
if i >= index and i < self.size():
last.setData(current.getData())
self.tail = last
self.currentSize -= 1
return True
# Will delete element if part of it
# contains the provided value.
def removeValueContains(self, value):
current = self.head
for i in xrange(0, self.size()-1):
last = current
current = current.getNext()
if str(last.getData()).lower().__contains__(str(value).lower()):
for j in xrange(i, self.size()-1):
last.setData(current.getData())
last = current
current = current.getNext()
self.tail = last
self.currentSize -= 1
return True
raise ValueNotFound
return False
# Will delete element if it equals
# to the provided value. Exact match.
def removeValueEquals(self, value):
current = self.head
for i in xrange(0, self.size()-1):
last = current
current = current.getNext()
if last.getData() == value:
for j in xrange(i, self.size()-1):
last.setData(current.getData())
last = current
current = current.getNext()
self.tail = last
self.currentSize -= 1
return True
raise ValueNotFound
return False
# Will return True if element in the Linked List
# equals to the provided value. Exact match.
def containsExactMatch(self, value):
current = self.head
for i in xrange(0, self.size()-1):
last = current
current = current.getNext()
if last.getData() == value:
return True
return False
# Will return True if part of some element in the Linked List
# contains the provided value.
def containsWithin(self, value):
current = self.head
for i in xrange(0, self.size()-1):
last = current
current = current.getNext()
if str(last.getData()).lower().__contains__(str(value).lower()):
return True
return False
def toArray(self):
list = []
current = self.head
for i in xrange(self.size()):
last = current
current = current.getNext()
list.append(last.getData())
return list
# To preserve the Pythonic way to iterate through collections,
# iterate basically converts linked list to array, which you
# can iterate through using Python's for loop.
# Example:
# "for item in myList.iterate():
# print item"
def iterate(self):
return self.toArray()
def indexOf(self, value):
current = self.head
for i in xrange(self.size()):
last = current
current = current.getNext()
if last.getData() == value:
return i
raise ValueNotFound
def lastIndexOf(self, value):
current = self.head
toReturn = -1
for i in xrange(self.size()):
last = current
current = current.getNext()
if last.getData() == value:
toReturn = i
if toReturn >= 0:
return toReturn
else:
raise ValueNotFound
def set(self, index, value):
if index < 0 or index > self.currentSize:
raise IndexError
if index == self.currentSize:
self.add(value)
return
current = self.head
for i in xrange(self.size()):
last = current
current = current.getNext()
if i == index:
last.setData(value)
class ValueNotFound(Exception):
pass