- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to create and display a doubly linked list
When it is required to create and display the elements in a doubly linked list, a ‘Node’ class needs to be created. In this class, there are three attributes, the data that is present in the node, the access to the next node of the linked list, and the access to the previous node of the linked list.
Below is a demonstration for the same −
class Node: def __init__(self, my_data): self.prev = None self.data = my_data self.next = None class double_list: def __init__(self): self.head = None self.tail = None def add_data(self, my_data): new_node = Node(my_data) if(self.head == None): self.head = self.tail = new_node; self.head.previous = None; self.tail.next = None; else: self.tail.next = new_node; new_node.previous = self.tail; self.tail = new_node; self.tail.next = None; def print_it(self): curr = self.head if (self.head == None): print("The list is empty") return print("The nodes in the doubly linked list are :") while curr != None: print(curr.data) curr = curr.next my_instance = double_list() print("Elements are being added to the doubly linked list") my_instance.add_data(10) my_instance.add_data(24) my_instance.add_data(54) my_instance.add_data(77) my_instance.add_data(92) my_instance.print_it()
Elements are being added to the doubly linked list The nodes in the doubly linked list are : 10 24 54 77 92
- The ‘Node’ class is created.
- Another class with required attributes is created.
- A method named ‘add_data’ is defined, that is used to add data to the doubly linked list.
- Another method named ‘print_it’ is defined, that displays the nodes of the circular linked list.
- An object of the ‘double_list’ class is created, and the methods are called on it to display the nodes of the doubly linked list.
- An ‘init’ method is defined, that the root, head, and tail nodes of the doubly linked list to None.
- These methods are called, and data is added to the list.
- This is displayed on the console using the ‘print_it’ method.
- Python program to create and display a Circular Linked List
- Python program to create a doubly linked list of n nodes and display it in reverse order
- Golang Program to create a doubly linked list and traverse forward.
- Python program to create a doubly linked list from a ternary tree
- C++ Program to Implement Doubly Linked List
- Python program to search an element in a doubly linked list
- Python program to create a doubly linked list of n nodes and count the number of nodes
- Python program to rotate doubly linked list by N nodes
- Python program to convert a given binary tree to doubly linked list
- Python Program to Create a Linked List & Display the Elements in the List
- C++ Program to Implement Circular Doubly Linked List
- C++ Program to Implement Sorted Doubly Linked List
- Python program to remove duplicate elements from a Doubly Linked List\n
- Python Program to Find the Largest Element in a Doubly Linked List
- Difference between Singly linked list and Doubly linked list in Java