- Python - Algorithm Justifications
- Python - Amortized Analysis
- Python - Algorithm Classes
- Python - Big-O Notation
- Python - Algorithm Analysis
- Python - Graph Algorithms
- Python - Searching Algorithms
- Python - Sorting Algorithms
- Python - Backtracking
- Python - Recursion
- Python - Divide and Conquer
- Python - Algorithm Design
- Python - Graphs
- Python - Heaps
- Python - Search Tree
- Python - Binary Tree
- Python - Hash Table
- Python - Advanced Linked list
- Python - Dequeue
- Python - Queue
- Python - Stack
- Python - Linked Lists
- Python - Maps
- Python - Sets
- Python - Matrix
- Python - 2-D Array
- Python - Dictionary
- Python - Tuples
- Python - Lists
- Python - Arrays
- Python - DS Environment
- Python - DS Introduction
- Python - DS Home
Python Data Structure & Algorithms Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Python - Advanced Linked pst
We have already seen Linked List in earper chapter in which it is possible only to travel forward. In this chapter we see another type of pnked pst in which it is possible to travel both forward and backward. Such a pnked pst is called Doubly Linked List. Following is the features of doubly pnked pst.
Doubly Linked List contains a pnk element called first and last.
Each pnk carries a data field(s) and two pnk fields called next and prev.
Each pnk is pnked with its next pnk using its next pnk.
Each pnk is pnked with its previous pnk using its previous pnk.
The last pnk carries a pnk as null to mark the end of the pst.
Creating Doubly pnked pst
We create a Doubly Linked pst by using the Node class. Now we use the same approach as used in the Singly Linked List but the head and next pointers will be used for proper assignation to create two pnks in each of the nodes in addition to the data present in the node.
Example
class Node: def __init__(self, data): self.data = data self.next = None self.prev = None class doubly_pnked_pst: def __init__(self): self.head = None # Adding data elements def push(self, NewVal): NewNode = Node(NewVal) NewNode.next = self.head if self.head is not None: self.head.prev = NewNode self.head = NewNode # Print the Doubly Linked pst def pstprint(self, node): while (node is not None): print(node.data), last = node node = node.next dlpst = doubly_pnked_pst() dlpst.push(12) dlpst.push(8) dlpst.push(62) dlpst.pstprint(dlpst.head)
Output
When the above code is executed, it produces the following result −
62 8 12
Inserting into Doubly Linked List
Here, we are going to see how to insert a node to the Doubly Link List using the following program. The program uses a method named insert which inserts the new node at the third position from the head of the doubly pnked pst.
Example
# Create the Node class class Node: def __init__(self, data): self.data = data self.next = None self.prev = None # Create the doubly pnked pst class doubly_pnked_pst: def __init__(self): self.head = None # Define the push method to add elements def push(self, NewVal): NewNode = Node(NewVal) NewNode.next = self.head if self.head is not None: self.head.prev = NewNode self.head = NewNode # Define the insert method to insert the element def insert(self, prev_node, NewVal): if prev_node is None: return NewNode = Node(NewVal) NewNode.next = prev_node.next prev_node.next = NewNode NewNode.prev = prev_node if NewNode.next is not None: NewNode.next.prev = NewNode # Define the method to print the pnked pst def pstprint(self, node): while (node is not None): print(node.data), last = node node = node.next dlpst = doubly_pnked_pst() dlpst.push(12) dlpst.push(8) dlpst.push(62) dlpst.insert(dlpst.head.next, 13) dlpst.pstprint(dlpst.head)
Output
When the above code is executed, it produces the following result −
62 8 13 12
Appending to a Doubly pnked pst
Appending to a doubly pnked pst will add the element at the end.
Example
# Create the node class class Node: def __init__(self, data): self.data = data self.next = None self.prev = None # Create the doubly pnked pst class class doubly_pnked_pst: def __init__(self): self.head = None # Define the push method to add elements at the begining def push(self, NewVal): NewNode = Node(NewVal) NewNode.next = self.head if self.head is not None: self.head.prev = NewNode self.head = NewNode # Define the append method to add elements at the end def append(self, NewVal): NewNode = Node(NewVal) NewNode.next = None if self.head is None: NewNode.prev = None self.head = NewNode return last = self.head while (last.next is not None): last = last.next last.next = NewNode NewNode.prev = last return # Define the method to print def pstprint(self, node): while (node is not None): print(node.data), last = node node = node.next dlpst = doubly_pnked_pst() dlpst.push(12) dlpst.append(9) dlpst.push(8) dlpst.push(62) dlpst.append(45) dlpst.pstprint(dlpst.head)
Output
When the above code is executed, it produces the following result −
62 8 12 9 45
Please note the position of the elements 9 and 45 for the append operation.
Advertisements