Code

Linked List
1
class Node:
2
def __init__(self, data):
3
self.data = data
4
self.next = None
5
6
class LinkedList:
7
def __init__(self):
8
self.head = None
9
10
def insert(self, data):
11
new_node = Node(data)
12
if self.head is None:
13
self.head = new_node
14
else:
15
temp = self.head
16
while temp.next:
17
temp = temp.next
18
temp.next = new_node
19
20
if __name__ == '__main__':
21
list = LinkedList()
22
23
list.insert(10)
24
list.insert(20)
25
list.insert(30)

Canvas

Code visualizations will appear here.

Algorithm Description

This program demonstrates the creation and basic functionality of a Singly Linked List. It includes a Node class to represent elements of the list and a LinkedList class to manage the list. The insert method appends new elements at the end of the list. This program initializes a linked list and inserts three elements into it.
Time Complxity:  O(n)

Real-Life Use Cases

  • Undo Functionality in Text Editors: Linked lists can store a series of actions in a text editor, where each node represents a change, allowing efficient traversal to undo or redo actions.
  • Music Playlists: In music players, linked lists can represent playlists where each song is a node, allowing easy insertion, deletion, and traversal for seamless playback control.
  • Navigating Browsers History: Browsers use a doubly linked list to manage the history of visited websites, enabling users to move back and forth between pages easily.
  • Real-Time Scheduling Systems: Linked lists are used in operating systems to manage processes and tasks dynamically, especially in priority or round-robin scheduling.