-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list.py
More file actions
311 lines (245 loc) · 7.6 KB
/
linked_list.py
File metadata and controls
311 lines (245 loc) · 7.6 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
"""
Singly Linked List implementation in Python.
This module provides two classes:
1. Node: Represents a node in the singly linked list.
2. LinkedList: Represents the linked list with operations:
- Append values at the end
- Insert values at a specific index
- Remove from beginning, end, by value, or by index
- Reverse the list
- Change a value at a given index
- Check if the list is empty
- String representation of the list
Author: [Ahmed Hisham]
License: MIT
"""
class Node:
"""
Represents a single node in the singly linked list.
Attributes:
Value (any): The value stored in the node.
next (Node): Reference to the next node in the list.
"""
def __init__(self, Value):
self.Value = Value
self.next = None
class LinkedList:
"""
Singly Linked List (SLL) implementation.
Attributes:
head (Node): The first node in the list.
tail (Node): The last node in the list.
items (int): The number of nodes in the list.
"""
def __init__(self):
"""Initialize an empty linked list."""
self.head = None
self.tail = None
self.items = 0
def append(self, value):
"""
Insert a value at the end of the list.
Args:
value (any): The value to insert.
Returns:
True if inserted successfully.
"""
new_node = Node(value)
if not self.head: # List is empty
self.head = self.tail = new_node
else:
self.tail.next = new_node
self.tail = new_node
self.items += 1
return True
def __str__(self):
"""
Get a string representation of the list.
Returns:
str: Values joined by " -> " arrows.
Raises:
Exception: If the list is empty.
"""
if not self.head:
raise Exception("List is empty")
current_node = self.head
values = []
while current_node:
values.append(str(current_node.Value))
current_node = current_node.next
return " -> ".join(values)
def insert(self, Value, index):
"""
Insert a value at a specific index.
Args:
Value (any): Value to insert.
index (int): Position to insert at (0-based).
Returns:
True if inserted successfully.
Raises:
Exception: If index is invalid.
"""
if index < 0 or index > self.items:
raise Exception("Invalid Index")
new_node = Node(Value)
self.items += 1
if index == 0: # Insert at beginning
new_node.next = self.head
self.head = new_node
if not self.tail: # List was empty
self.tail = new_node
else:
current_node = self.head
for _ in range(index - 1):
current_node = current_node.next
new_node.next = current_node.next
current_node.next = new_node
if new_node.next is None: # Inserted at end
self.tail = new_node
return True
def pop(self):
"""
Remove the last element from the list.
Returns:
The value of the removed node.
Raises:
Exception: If the list is empty.
"""
if not self.head:
raise Exception("List is empty")
if self.items == 1: # Only one element
pop_node = self.tail
self.head = self.tail = None
self.items -= 1
return pop_node.Value
current_node = self.head
while current_node.next != self.tail:
current_node = current_node.next
pop_node = self.tail
self.tail = current_node
current_node.next = None
self.items -= 1
return pop_node.Value
def change_value(self, Value, index):
"""
Change the value at a specific index.
Args:
Value (any): The new value.
index (int): Position of the node to update.
Returns:
True if updated successfully.
Raises:
Exception: If the list is empty or index is invalid.
"""
if not self.head:
raise Exception("List is empty")
if index < 0 or index >= self.items:
raise Exception("Invalid Index")
current_node = self.head
for _ in range(index):
current_node = current_node.next
current_node.Value = Value
return True
def is_empty(self):
"""
Check if the list is empty.
Returns:
True if empty, False otherwise.
"""
return self.items == 0
def delete_front(self):
"""
Remove the first element from the list.
Returns:
The removed value.
Raises:
Exception: If the list is empty.
"""
if self.is_empty():
raise Exception("List is empty")
current = self.head
if self.items == 1:
self.head = self.tail = None
else:
self.head = self.head.next
current.next = None
self.items -= 1
return current.Value
def delete_value(self, value):
"""
Remove a node by value.
Args:
value (any): The value to remove.
Returns:
The removed value if found, -1 otherwise.
Raises:
Exception: If the list is empty.
"""
if self.is_empty():
raise Exception("List is empty")
if self.head.Value == value:
return self.delete_front()
if self.tail.Value == value:
return self.pop()
current_node = self.head.next
prev_node = self.head
while current_node and current_node.Value != value:
prev_node = current_node
current_node = current_node.next
if current_node and current_node.Value == value:
prev_node.next = current_node.next
self.items -= 1
return value
else:
return -1
def delete_index(self, index):
"""
Remove a node by index.
Args:
index (int): The index of the node to remove.
Returns:
The removed value if found, True otherwise.
Raises:
Exception: If list is empty or index is invalid.
"""
if index < 0 or index >= self.items:
raise Exception("Invalid Index")
if self.is_empty():
raise Exception("List is empty")
if index == 0:
return self.delete_front()
elif index == (self.items - 1):
return self.pop()
current_node = self.head.next
prev_node = self.head
i = 1
while current_node and i != index:
current_node = current_node.next
prev_node = prev_node.next
i += 1
if i == index:
prev_node.next = current_node.next
self.items -= 1
return True
else:
return -1
def reverse(self):
"""
Reverse the entire list in-place.
Returns:
True if reversed successfully.
"""
if self.is_empty():
raise Exception("List is empty")
if self.items == 1:
return True
prev = None
curr = self.head
while curr:
next_node = curr.next
curr.next = prev
prev = curr
curr = next_node
self.tail = self.head
self.head = prev
return True