|
| 1 | +# Linked List Data Structure |
| 2 | + |
| 3 | +Link list is a linear data Structure which can be defined as collection of objects called nodes that are randomly stored in the memory. |
| 4 | +A node contains two types of metadata i.e. data stored at that particular address and the pointer which contains the address of the next node in the memory. |
| 5 | +The last node of the list contains pointer to the null. |
| 6 | + |
| 7 | +## Why use linked list over array? |
| 8 | + |
| 9 | +From the beginning, we are using array data structure to organize the group of elements that are stored individually in the memory. |
| 10 | +However, there are some advantage and disadvantage of array which should be known to decide which data structure will used throughout the program. |
| 11 | + |
| 12 | +limitations |
| 13 | + |
| 14 | +1. The size of array must be known in advance before using it in the program. |
| 15 | +2. Increasing size of the array is a time taking process. It is almost impossible to expand the size of the array at run time. |
| 16 | +3. All the elements in the array need to be contiguously stored in the memory. Inserting any element in the array needs shifting of all its predecessors. |
| 17 | + |
| 18 | +So we introduce a new data structure to overcome these limitations. |
| 19 | + |
| 20 | +Linked list is used because, |
| 21 | +1. It allocates the memory dynamically. All the nodes of linked list are non-contiguously stored in the memory and linked together with the help of pointers. |
| 22 | +2. Sizing is no longer a problem since we do not need to define its size at the time of declaration. List grows as per the program's demand and limited to the available memory space. |
| 23 | + |
| 24 | +Let's code something |
| 25 | + |
| 26 | +The smallest Unit: Node |
| 27 | + |
| 28 | +class Node: |
| 29 | + def __init__(self, data): |
| 30 | + self.data = data # Assigns the given data to the node |
| 31 | + self.next = None # Initialize the next attribute to null |
| 32 | + |
| 33 | +Now, we will see the types of linked list. |
| 34 | + |
| 35 | +There are mainly four types of linked list, |
| 36 | +1. Singly Link list |
| 37 | +2. Doubly link list |
| 38 | +3. Circular link list |
| 39 | +4. Doubly circular link list |
| 40 | + |
| 41 | + |
| 42 | +## 1. Singly linked list. |
| 43 | + |
| 44 | +Simply think it is a chain of nodes in which each node remember(contains) the addresses of it next node. |
| 45 | + |
| 46 | +### Creating a linked list class |
| 47 | + |
| 48 | +class LinkedList: |
| 49 | + def __init__(self): |
| 50 | + self.head = None # Initialize head as None |
| 51 | + |
| 52 | +### Inserting a new node at the beginning of a linked list |
| 53 | + |
| 54 | + def insertAtBeginning(self, new_data): |
| 55 | + new_node = Node(new_data) # Create a new node |
| 56 | + new_node.next = self.head # Next for new node becomes the current head |
| 57 | + self.head = new_node # Head now points to the new node |
| 58 | + |
| 59 | +### Inserting a new node at the end of a linked list |
| 60 | + |
| 61 | + def insertAtEnd(self, new_data): |
| 62 | + new_node = Node(new_data) # Create a new node |
| 63 | + if self.head is None: |
| 64 | + self.head = new_node # If the list is empty, make the new node the head |
| 65 | + return |
| 66 | + last = self.head |
| 67 | + while last.next: # Otherwise, traverse the list to find the last node |
| 68 | + last = last.next |
| 69 | + last.next = new_node # Make the new node the next node of the last node |
| 70 | + |
| 71 | +### Inserting a new node at the middle of a linked list |
| 72 | + |
| 73 | + def insertAtPosition(self, data, position): |
| 74 | + new_node = Node(data) |
| 75 | + if position <= 0: #check if position is valid or not |
| 76 | + print("Position should be greater than 0") |
| 77 | + return |
| 78 | + if position == 1: |
| 79 | + new_node.next = self.head |
| 80 | + self.head = new_node |
| 81 | + return |
| 82 | + current_node = self.head |
| 83 | + current_position = 1 |
| 84 | + while current_node and current_position < position - 1: #Iterating to behind of the postion. |
| 85 | + current_node = current_node.next |
| 86 | + current_position += 1 |
| 87 | + if not current_node: #Check if Position is out of bound or not |
| 88 | + print("Position is out of bounds") |
| 89 | + return |
| 90 | + new_node.next = current_node.next #connect the intermediate node |
| 91 | + current_node.next = new_node |
| 92 | + |
| 93 | +### Printing the Linked list |
| 94 | + |
| 95 | + def printList(self): |
| 96 | + temp = self.head # Start from the head of the list |
| 97 | + while temp: |
| 98 | + print(temp.data,end=' ') # Print the data in the current node |
| 99 | + temp = temp.next # Move to the next node |
| 100 | + print() # Ensures the output is followed by a new line |
| 101 | + |
| 102 | + |
| 103 | +Lets complete the code and create a linked list. |
| 104 | + |
| 105 | +Connect all the code. |
| 106 | + |
| 107 | +if __name__ == '__main__': |
| 108 | + llist = LinkedList() |
| 109 | + |
| 110 | + # Insert words at the beginning |
| 111 | + llist.insertAtBeginning(4) # <4> |
| 112 | + llist.insertAtBeginning(3) # <3> 4 |
| 113 | + llist.insertAtBeginning(2) # <2> 3 4 |
| 114 | + llist.insertAtBeginning(1) # <1> 2 3 4 |
| 115 | + |
| 116 | + # Insert a word at the end |
| 117 | + llist.insertAtEnd(10) # 1 2 3 4 <10> |
| 118 | + llist.insertAtEnd(7) # 1 2 3 4 10 <7> |
| 119 | + |
| 120 | + #Insert at a random position |
| 121 | + llist.insertAtPosition(9,4) ## 1 2 3 <9> 4 10 7 |
| 122 | + # Print the list |
| 123 | + llist.printList() |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | + output: |
| 128 | + 1 2 3 9 4 10 7 |
| 129 | + |
| 130 | + |
| 131 | +### Deleting a node from the beginning of a linked list |
| 132 | +check the list is empty otherwise shift the head to next node. |
| 133 | + |
| 134 | +def deleteFromBeginning(self): |
| 135 | + if self.head is None: |
| 136 | + return "The list is empty" # If the list is empty, return this string |
| 137 | + self.head = self.head.next # Otherwise, remove the head by making the next node the new head |
| 138 | + |
| 139 | +### Deleting a node from the end of a linked list |
| 140 | + |
| 141 | +def deleteFromEnd(self): |
| 142 | + if self.head is None: |
| 143 | + return "The list is empty" |
| 144 | + if self.head.next is None: |
| 145 | + self.head = None # If there's only one node, remove the head by making it None |
| 146 | + return |
| 147 | + temp = self.head |
| 148 | + while temp.next.next: # Otherwise, go to the second-last node |
| 149 | + temp = temp.next |
| 150 | + temp.next = None # Remove the last node by setting the next pointer of the second-last node to None |
| 151 | + |
| 152 | + |
| 153 | +### Search in a linked list |
| 154 | + |
| 155 | +def search(self, value): |
| 156 | + current = self.head # Start with the head of the list |
| 157 | + position = 0 # Counter to keep track of the position |
| 158 | + while current: # Traverse the list |
| 159 | + if current.data == value: # Compare the list's data to the search value |
| 160 | + return f"Value '{value}' found at position {position}" # Print the value if a match is found |
| 161 | + current = current.next |
| 162 | + position += 1 |
| 163 | + return f"Value '{value}' not found in the list" |
| 164 | + |
| 165 | + |
| 166 | + |
| 167 | + |
| 168 | + |
| 169 | + |
| 170 | + |
| 171 | + |
| 172 | + |
| 173 | + |
| 174 | + |
| 175 | + |
| 176 | + |
| 177 | + |
| 178 | + |
| 179 | + |
| 180 | + |
| 181 | + |
0 commit comments