-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.py
More file actions
236 lines (201 loc) · 7.37 KB
/
LinkedList.py
File metadata and controls
236 lines (201 loc) · 7.37 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
class LinkedList:
def __init__(self):
self.__head = None
self.__tail = None
self.__size = 0
# Return the head element in the list
def getFirst(self):
if self.__size == 0:
return None
else:
return self.__head.element
# Return the last element in the list
def getLast(self):
if self.__size == 0:
return None
else:
return self.__tail.element
# Add an element to the beginning of the list
def addFirst(self, e):
newNode = Node(e) # Create a new node
newNode.next = self.__head # link the new node with the head
self.__head = newNode # head points to the new node
self.__size += 1 # Increase list size
if self.__tail == None: # the new node is the only node in list
self.__tail = self.__head
# Add an element to the end of the list
def addLast(self, e):
newNode = Node(e) # Create a new node for e
if self.__tail == None:
self.__head = self.__tail = newNode # The only node in list
else:
self.__tail.next = newNode # Link the new with the last node
self.__tail = self.__tail.next # tail now points to the last node
self.__size += 1 # Increase size
# Same as addLast
def add(self, e):
self.addLast(e)
# Insert a new element at the specified index in this list
# The index of the head element is 0
def insert(self, index, e):
if index == 0:
self.addFirst(e) # Insert first
elif index >= self.__size:
self.addLast(e) # Insert last
else: # Insert in the middle
current = self.__head
for i in range(1, index):
current = current.next
temp = current.next
current.next = Node(e)
(current.next).next = temp
self.__size += 1
# Remove the head node and
# return the object that is contained in the removed node.
def removeFirst(self):
if self.__size == 0:
return None # Nothing to delete
else:
temp = self.__head # Keep the first node temporarily
self.__head = self.__head.next # Move head to point the next node
self.__size -= 1 # Reduce size by 1
if self.__head == None:
self.__tail = None # List becomes empty
return temp.element # Return the deleted element
# Remove the last node and
# return the object that is contained in the removed node
def removeLast(self):
if self.__size == 0:
return None # Nothing to remove
elif self.__size == 1: # Only one element in the list
temp = self.__head
self.__head = self.__tail = None # list becomes empty
self.__size = 0
return temp.element
else:
current = self.__head
for i in range(self.__size - 2):
current = current.next
temp = self.__tail
self.__tail = current
self.__tail.next = None
self.__size -= 1
return temp.element
# Remove the element at the specified position in this list.
# Return the element that was removed from the list.
def removeAt(self, index):
if index < 0 or index >= self.__size:
return None # Out of range
elif index == 0:
return self.removeFirst() # Remove first
elif index == self.__size - 1:
return self.removeLast() # Remove last
else:
previous = self.__head
for i in range(1, index):
previous = previous.next
current = previous.next
previous.next = current.next
self.__size -= 1
return current.element
# Return true if the list is empty
def isEmpty(self):
return self.__size == 0
# Return the size of the list
def getSize(self):
return self.__size
def __str__(self):
result = "["
current = self.__head
for i in range(self.__size):
result += str(current.element)
current = current.next
if current != None:
result += ", " # Separate two elements with a comma
else:
result += "]" # Insert the closing ] in the string
return result
# Clear the list */
def clear(self):
self.__head = self.__tail = None
# Return true if this list contains the element o
def contains(self, e):
current=self.__head
for i in range(self.__size):
if current.element == e:
return True
current=current.next
# Remove the element and return true if the element is in the list
def remove(self, e):
if contains(self, e) :
current = self.__head
for i in range(indexOf(e)-1):
current=current.next
current.next=current.next.next
return True
# Return the element from this list at the specified index
def get(self, index):
thenode=self.__head
for i in range(index):
thenode=thenode.next
print(thenode.element)
return None
# Return the index of the head matching element in this list.
# Return -1 if no match.
def indexOf(self, e):
current=self.__head
indexes=[]
count=0
if contains(self, e):
for i in range(self.__size):
if current.element != e:
current = current.next
count +=1
indexes.append(count)
else:
indexes.append('-1')
return indexes[0]
# Return the index of the last matching element in this list
# Return -1 if no match.
def lastIndexOf(self, e):
current = self.__head
indexes = []
count = 0
if contains(self, e):
for i in range(self.__size):
if current.element != e:
current = current.next
count += 1
indexes.append(count)
else:
indexes.append('-1')
return indexes[-1]
# Replace the element at the specified position in this list
# with the specified element. */
def set(self, index, e):
current=self.__head
for i in range(index):
current=current.next
current.element=e
return None
# Return elements via indexer
def __getitem__(self, index):
return self.get(index)
# Return an iterator for a linked list
def __iter__(self):
return LinkedListIterator(self.__head)
# The Node class
class Node:
def __init__(self, element):
self.element = element
self.next = None
class LinkedListIterator:
def __init__(self, head):
self.current = head
def __next__(self):
if self.current == None:
raise StopIteration
else:
element = self.current.element
self.current = self.current.next
return element