Linked list is a data structure that is used to store data linking one with another. You can think it like a chain of elements. Each element points to the next element and each element holds two properties : one value and pointer to the next element in the list.
These elements are called Node. So, we have a chain of nodes with each node points to its next node in a linked list.
The first node is called Head Node. It points to the first element of the Linked List. The last node of the linked list points to None.
In this post, we will learn how to implement linked list in Python. We will write one program to :
- Create a linked list by taking user input
- Iterate through the linked list
Below is the complete python program to
class Node: def __init__(self, value=None): self.value = value self.next = None class LinkedList: def __init__(self): self.head = None def add(self, value): new_node = Node(value) if self.head == None: self.head = new_node else: new_node.next = self.head self.head = new_node def print_all(self): current = self.head while current is not None: print(current.value) current = current.next if __name__ == "__main__" : linked_list = LinkedList() current_selection = 0 while current_selection != -1: current_selection = int(input("Enter 1 to add value, 2 to print all and -1 to exit : ")) if current_selection == 1: total_values = int(input("Enter total values : ")) for i in range(0, total_values): value_to_add = int(input("Enter value to add : ")) linked_list.add(value_to_add) if current_selection == 2: linked_list.print_all()
- LinkedList is the class to create a linked list. When we are initializing this class, we are assigning the value of head as None.
- LinkedList class has two methods : add to add a value and print_all to print all values of the linked list.
- Node class is used to create one linked list Node. This class assign the value to the node and assign None to next.
- The add method creates one Node and adds it to the start of the linked list. If the value of head is None, it adds this Node to it. Else, it adds this value to head and assign next of it to the already pointed Node. i.e. we are adding the new node to the start of the linked list.
- print_all method prints all values of the linked list. We are storing the value of head in current and using a while loop, we are iterating all nodes to the end.
- Inside the main method, we are creating one linked list and adding values by taking user inputs. The inside while loop is an infinite while loop and it is used to take user inputs repeatedly.
Enter 1 to add value, 2 to print all and -1 to exit : 1 Enter total values : 4 Enter value to add : 1 Enter value to add : 2 Enter value to add : 3 Enter value to add : 4 Enter 1 to add value, 2 to print all and -1 to exit : 2 4 3 2 1 Enter 1 to add value, 2 to print all and -1 to exit : -1 Enter 1 to add value, 2 to print all and -1 to exit : 1 Enter total values : 4 Enter value to add : 10 Enter value to add : 20 Enter value to add : 30 Enter value to add : 40 Enter 1 to add value, 2 to print all and -1 to exit : 2 40 30 20 10 Enter 1 to add value, 2 to print all and -1 to exit : -1
- Python program to read all numbers from a file
- Python program to find the sum of all numbers in a file
- Python program to append a single line to the end of a file
- Difference between python append() and extend() methods of list
- How to check if a triangle is valid or not in python
- Find all prime numbers in a range in python