- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 insert a new node at the beginning of the Doubly Linked list
When it is required to insert a new node at the beginning of 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 −
Example
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_at_start(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.previous = new_node new_node.next = self.head new_node.previous = None self.head = new_node 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 beginning of doubly linked list") my_instance.add_data_at_start(10) my_instance.print_it() my_instance.add_data_at_start(24) my_instance.print_it() my_instance.add_data_at_start(54) my_instance.print_it() my_instance.add_data_at_start(77) my_instance.print_it() my_instance.add_data_at_start(92) my_instance.print_it()
Output
Elements are being added to the beginning of doubly linked list The nodes in the doubly linked list are : 10 The nodes in the doubly linked list are : 24 10 The nodes in the doubly linked list are : 54 24 10 The nodes in the doubly linked list are : 77 54 24 10 The nodes in the doubly linked list are : 92 77 54 24 10
Explanation
- The ‘Node’ class is created.
- Another class with required attributes is created.
- A method named ‘add_data_at_start’ is defined, that is used to add data at the beginning of 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 add data to the beginning of the doubly linked list.
- An ‘init’ method is defined, that the root, head, and tail nodes of the doubly linked list to None.
- The list is iterated over, and the element is added to the beginning of the doubly linked list.
- This is displayed on the console using the ‘print_it’ method.
- Related Articles
- Python program to insert a new node at the end of the Doubly Linked List
- Python program to insert a new node at the middle of the Doubly Linked List
- Python program to insert a new node at the beginning of the Circular Linked List
- Python program to delete a new node from the beginning of the doubly linked list
- Python program to insert a new node at the end of the Circular Linked List
- Python program to insert a new node at the middle of the Circular Linked List
- Python program to delete a new node from the end of the doubly linked list
- Python program to delete a new node from the middle of the doubly linked list
- Write a program in C++ to insert a Node at the beginning of the given Singly linked list
- C# Program to remove a node at the beginning of a Linked List
- Golang program to insert a new node after the Kth node (K is not in the linked list)
- Python program to find the maximum and minimum value node from a doubly linked list
- C program to insert a node at any position using double linked list
- Golang Program to insert a node at the ith index node, when the index is at the 0th position in the linked list.
- Golang program to insert a node at the ith index node, when the index is at the last position in the linked list.

Advertisements