- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
Python Program to Create a Linked List & Display the Elements in the List
When it is required to create a linked list, and display the elements of this linked list, a method to add values to the linked list, as well as a method to display the elements of a Linked List.
Below is a demonstration for the same −
class Node: def __init__(self, data): self.data = data self.next = None class my_linked_list: def __init__(self): self.head = None self.last_node = None def add_value(self, my_data): if self.last_node is None: self.head = Node(my_data) self.last_node = self.head else: self.last_node.next = Node(my_data) self.last_node = self.last_node.next def print_it(self): curr = self.head while curr is not None: print(curr.data) curr = curr.next my_instance = my_linked_list() n = int(input('How many elements should be added ? ')) for i in range(n): data = int(input('Enter a data value : ')) my_instance.add_value(data) print('The linked list is : ') my_instance.print_it()
How many elements should be added ? 4 Enter a data value : 6 Enter a data value : 7 Enter a data value : 8 Enter a data value : 9 The linked list is : 6 7 8 9
The ‘Node’ class is created.
Another ‘my_linked_list’ class with required attributes is created.
It has an ‘init’ function that is used to initialize the first element, i.e the ‘head’ to ‘None’ and last node to ‘None’.
Another method named ‘add_value’ is defined, that is used to add data to the linked list.
Another method named ‘print_it’ is defined that is used to display the linked list data on the console.
An object of the ‘my_linked_list’ class is created.
The user input is taken for the number of elements in the linked list.
This range is iterated over, and the methods are called on it to add data.
This is displayed on the console using the ‘print_it’ method.
- Related Articles
- Python program to create and display a doubly linked list
- Python program to create and display a Circular Linked List
- Python Program To Add Elements To A Linked List
- Python Program to Display all the Nodes in a Linked List using Recursion
- Python program to sort the elements of the Circular Linked List
- Python Program to Display the Nodes of a Linked List in Reverse using Recursion
- Python program to create a doubly linked list of n nodes and display it in reverse order
- Python program to create a Circular Linked List of n nodes and display it in reverse order
- Python Program to Display the Nodes of a Linked List in Reverse without using Recursion
- Python program to remove duplicate elements from a Circular Linked List
- Program to create linked list to binary search tree in Python
- Golang program to remove elements from the linked list
- Python Program to Detect the Cycle in a Linked List
- Program to find linked list intersection from two linked list in Python
- Program to interleave list elements from two linked lists in Python