In this program, you’ll be learning how to create and display a doubly linked list.
Doubly Linked List is a variation of the linked list. it’s a data structure that consists of a set of the sequential linked list which is tearmd as the node. (Two ends of the link list can also be called as a node).
And each node contains the 3 fields which are, two Link fields (as each node jas two ends) and one data field.
The first node of a list is called as head if a link list and end is called as the tail of the link list.
Program to create and display a doubly linked list.
class Node: def __init__(self,data): self.data = data; self.previous = None; self.next = None; class DoublyLinkedList: def __init__(self): self.head = None; self.tail = None; def addNode(self, data): newNode = Node(data); if(self.head == None): self.head = self.tail = newNode; self.head.previous = None; self.tail.next = None; else: self.tail.next = newNode; newNode.previous = self.tail; self.tail = newNode; self.tail.next = None; def display(self): current = self.head; if(self.head == None): print("List is empty"); return; print("Nodes of doubly linked list: "); while(current != None): print(current.data),; current = current.next; dList = DoublyLinkedList(); dList.addNode(1); dList.addNode(2); dList.addNode(3); dList.addNode(4); dList.addNode(5); dList.display();
Output
Nodes of doubly linked list: 1 2 3 4 5
Related Program
- Python Program to Find LCM
- Python Program to Find HCF or GCD
- Python Program To Display Powers of 2 Using Anonymous Function
- Python Program to Display Calendar
- Python Program to find the Square Roots
- Python Program to Shuffle Deck of Cards
- Python program convert decimal number to binary number using recursion
- Python Program to Find Factorial of a Number Using Recursion
- Python Program to Check if a Number is Positive, Negative or 0
- Python Program to Generate a Random Alphanumeric String
Ask your questions and clarify your/others doubts on how you can create and display double linked list by commenting. Python Documentation