-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list.py
More file actions
530 lines (432 loc) Β· 13.1 KB
/
Copy pathlinked_list.py
File metadata and controls
530 lines (432 loc) Β· 13.1 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
# ============================================================
# Building a Linked List - Section by Section Demo
# Covers:
# 1. Linked List vs Array
# 2. Adding Nodes to a Linked List
# 3. Removing Nodes from a Linked List
# 4. Searching for a Node in a Linked List
# 5. Traversing a Linked List
# 6. Inserting a Node at a Specific Position
# 7. Deleting a Node at a Specific Position
# 8. Updating a Node at a Specific Position
# 9. Sorting a Linked List
# ============================================================
import time
# ============================================================
# SECTION 1: LINKED LIST VS ARRAY
# ============================================================
def section1_linked_list_vs_array():
print("Array: [10][20][30][40]")
print("LL: [10]->[20]->[30]->[40]->None")
time.sleep(1.5)
print("\nβ
SECTION 1 COMPLETE!")
time.sleep(1)
# ============================================================
# SECTION 2: ADDING NODES TO A LINKED LIST
# ============================================================
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
self.size = 0
def visualize(self):
if self.head is None:
print("Empty: []")
return
current = self.head
while current:
print(f"[{current.data}]", end=" -> ")
current = current.next
print("None")
def section2_adding_nodes():
print("=" * 50)
print("π SECTION 2: ADDING NODES")
print("=" * 50)
ll = LinkedList()
print("Created empty list")
ll.visualize()
time.sleep(1)
print("\nβ INSERT AT BEGINNING:")
print("Adding 10...")
time.sleep(0.8)
new_node = Node(10)
new_node.next = ll.head
ll.head = new_node
ll.size += 1
print("Step 1: Create node [10]")
time.sleep(0.8)
print("Step 2: Point to head")
time.sleep(0.8)
print("Step 3: Update head")
time.sleep(0.8)
ll.visualize()
time.sleep(1)
print("\nAdding 20 at beginning...")
time.sleep(0.8)
new_node2 = Node(20)
new_node2.next = ll.head
ll.head = new_node2
ll.size += 1
ll.visualize()
time.sleep(1)
print("\nβ INSERT AT END:")
print("Adding 30 at end...")
time.sleep(0.8)
new_node3 = Node(30)
if ll.head is None:
ll.head = new_node3
else:
current = ll.head
while current.next:
current = current.next
current.next = new_node3
ll.size += 1
print("Step 1: Create node [30]")
time.sleep(0.8)
print("Step 2: Find last node")
time.sleep(0.8)
print("Step 3: Link to new node")
time.sleep(0.8)
ll.visualize()
time.sleep(1)
print("\nβ
SECTION 2 COMPLETE!")
time.sleep(1)
# ============================================================
# SECTION 3: REMOVING NODES FROM A LINKED LIST
# ============================================================
def section3_removing_nodes():
print("=" * 50)
print("ποΈ SECTION 3: REMOVING NODES")
print("=" * 50)
# Setup test data
ll = LinkedList()
values = [10, 20, 30, 40]
for value in values:
new_node = Node(value)
if ll.head is None:
ll.head = new_node
else:
current = ll.head
while current.next:
current = current.next
current.next = new_node
ll.size += 1
print("Starting with:")
ll.visualize()
time.sleep(1)
print("\nποΈ DELETE FROM BEGINNING:")
print("Deleting first node...")
time.sleep(0.8)
if ll.head:
deleted = ll.head.data
ll.head = ll.head.next
ll.size -= 1
print(f"Deleted [{deleted}]")
time.sleep(0.8)
ll.visualize()
time.sleep(1)
print("\nποΈ DELETE FROM END:")
print("Deleting last node...")
time.sleep(0.8)
if ll.head and ll.head.next:
current = ll.head
while current.next.next:
current = current.next
deleted = current.next.data
current.next = None
ll.size -= 1
print(f"Deleted [{deleted}]")
time.sleep(0.8)
elif ll.head:
deleted = ll.head.data
ll.head = None
ll.size -= 1
print(f"Deleted [{deleted}]")
time.sleep(0.8)
ll.visualize()
time.sleep(1)
print("\nβ
SECTION 3 COMPLETE!")
time.sleep(1)
# ============================================================
# SECTION 4: SEARCHING FOR A NODE
# ============================================================
def section4_searching():
print("=" * 50)
print("π SECTION 4: SEARCHING")
print("=" * 50)
# Setup test data
ll = LinkedList()
values = [10, 20, 30, 40, 50]
for value in values:
new_node = Node(value)
if ll.head is None:
ll.head = new_node
else:
current = ll.head
while current.next:
current = current.next
current.next = new_node
ll.size += 1
print("Searching in:")
ll.visualize()
time.sleep(1)
target = 30
print(f"\nπ SEARCHING FOR {target}:")
time.sleep(0.8)
current = ll.head
position = 0
while current:
print(f"Checking position {position}: [{current.data}]", end="")
time.sleep(0.6)
if current.data == target:
print(" β FOUND!")
print(f"Found {target} at position {position}")
break
else:
print(" β")
current = current.next
position += 1
else:
print(f"\n{target} not found")
time.sleep(1)
print("\nβ
SECTION 4 COMPLETE!")
time.sleep(1)
# ============================================================
# SECTION 5: TRAVERSING A LINKED LIST
# ============================================================
def section5_traversing():
print("=" * 50)
print("πΆ SECTION 5: TRAVERSING")
print("=" * 50)
# Setup test data
ll = LinkedList()
values = [10, 20, 30, 40]
for value in values:
new_node = Node(value)
if ll.head is None:
ll.head = new_node
else:
current = ll.head
while current.next:
current = current.next
current.next = new_node
ll.size += 1
print("Traversing:")
ll.visualize()
time.sleep(1)
print("\nπΆ STEP-BY-STEP TRAVERSAL:")
time.sleep(0.8)
current = ll.head
position = 0
while current:
print(f"Position {position}: [{current.data}]", end="")
time.sleep(0.6)
if current.next:
print(" -> next")
else:
print(" -> None (end)")
current = current.next
position += 1
time.sleep(1)
print("\nβ
SECTION 5 COMPLETE!")
time.sleep(1)
# ============================================================
# SECTION 6: INSERTING AT SPECIFIC POSITION
# ============================================================
def section6_insert_at_position():
print("=" * 50)
print("π SECTION 6: INSERT AT POSITION")
print("=" * 50)
# Setup test data
ll = LinkedList()
values = [10, 20, 40, 50]
for value in values:
new_node = Node(value)
if ll.head is None:
ll.head = new_node
else:
current = ll.head
while current.next:
current = current.next
current.next = new_node
ll.size += 1
print("Starting with:")
ll.visualize()
time.sleep(1)
position = 2
data = 30
print(f"\nπ INSERTING {data} AT POSITION {position}:")
time.sleep(0.8)
new_node = Node(data)
current = ll.head
for i in range(position - 1):
print(f"Moving to position {i+1}: [{current.data}]")
current = current.next
time.sleep(0.6)
print(f"Found position {position-1}: [{current.data}]")
time.sleep(0.8)
new_node.next = current.next
current.next = new_node
ll.size += 1
print(f"Inserted [{data}] between [{current.data}] and next")
time.sleep(0.8)
ll.visualize()
time.sleep(1)
print("\nβ
SECTION 6 COMPLETE!")
time.sleep(1)
# ============================================================
# SECTION 7: DELETING AT SPECIFIC POSITION
# ============================================================
def section7_delete_at_position():
print("=" * 50)
print("ποΈ SECTION 7: DELETE AT POSITION")
print("=" * 50)
# Setup test data
ll = LinkedList()
values = [10, 20, 30, 40, 50]
for value in values:
new_node = Node(value)
if ll.head is None:
ll.head = new_node
else:
current = ll.head
while current.next:
current = current.next
current.next = new_node
ll.size += 1
print("Starting with:")
ll.visualize()
time.sleep(1)
position = 2
print(f"\nποΈ DELETING AT POSITION {position}:")
time.sleep(0.8)
current = ll.head
for i in range(position - 1):
print(f"Moving to position {i+1}: [{current.data}]")
current = current.next
time.sleep(0.6)
deleted_data = current.next.data
print(f"Found node to delete: [{deleted_data}]")
time.sleep(0.8)
current.next = current.next.next
ll.size -= 1
print(f"Deleted [{deleted_data}]")
time.sleep(0.8)
ll.visualize()
time.sleep(1)
print("\nβ
SECTION 7 COMPLETE!")
time.sleep(1)
# ============================================================
# SECTION 8: UPDATING A NODE
# ============================================================
def section8_updating():
print("=" * 50)
print("π SECTION 8: UPDATING NODES")
print("=" * 50)
# Setup test data
ll = LinkedList()
values = [10, 20, 30, 40]
for value in values:
new_node = Node(value)
if ll.head is None:
ll.head = new_node
else:
current = ll.head
while current.next:
current = current.next
current.next = new_node
ll.size += 1
print("Starting with:")
ll.visualize()
time.sleep(1)
position = 2
new_data = 25
print(f"\nπ UPDATING POSITION {position} TO {new_data}:")
time.sleep(0.8)
current = ll.head
for i in range(position):
print(f"Moving to position {i+1}: [{current.data}]")
current = current.next
time.sleep(0.6)
old_data = current.data
print(f"Found node: [{old_data}]")
time.sleep(0.8)
current.data = new_data
print(f"Updated from [{old_data}] to [{new_data}]")
time.sleep(0.8)
ll.visualize()
time.sleep(1)
print("\nβ
SECTION 8 COMPLETE!")
time.sleep(1)
# ============================================================
# SECTION 9: SORTING A LINKED LIST
# ============================================================
def section9_sorting():
print("=" * 50)
print("π SECTION 9: SORTING")
print("=" * 50)
# Setup test data
ll = LinkedList()
values = [30, 10, 40, 20]
for value in values:
new_node = Node(value)
if ll.head is None:
ll.head = new_node
else:
current = ll.head
while current.next:
current = current.next
current.next = new_node
ll.size += 1
print("Starting with (unsorted):")
ll.visualize()
time.sleep(1)
print("\nπ BUBBLE SORT:")
time.sleep(0.8)
swapped = True
pass_num = 1
while swapped:
swapped = False
current = ll.head
print(f"\nPass {pass_num}:")
time.sleep(0.8)
while current.next:
print(f"Comparing [{current.data}] and [{current.next.data}]", end="")
time.sleep(0.6)
if current.data > current.next.data:
current.data, current.next.data = current.next.data, current.data
swapped = True
print(" β Swapped!")
else:
print(" β")
current = current.next
if swapped:
print(f"After pass {pass_num}: {ll}")
time.sleep(0.8)
pass_num += 1
print(f"\nFinal sorted list:")
ll.visualize()
time.sleep(1)
print("\nβ
SECTION 9 COMPLETE!")
time.sleep(1)
if __name__ == "__main__":
section1_linked_list_vs_array()
print("\n" + "="*60 + "\n")
section2_adding_nodes()
print("\n" + "="*60 + "\n")
section3_removing_nodes()
print("\n" + "="*60 + "\n")
section4_searching()
print("\n" + "="*60 + "\n")
section5_traversing()
print("\n" + "="*60 + "\n")
section6_insert_at_position()
print("\n" + "="*60 + "\n")
section7_delete_at_position()
print("\n" + "="*60 + "\n")
section8_updating()
print("\n" + "="*60 + "\n")
section9_sorting()