Doubly Linked List in Python: Objective
Doubly Linked List in Python: Objective
Objective
– Understand the concepts of doubly linked lists
– Implement doubly linked list
Linked Lists:
Unlike a single linked list, the doubly linked list can be traversed and searched in both directions.
The reference to the next node helps in traversing the node in the forward direction while the
references to the previous nodes allow traversal in the backward direction.
Basic operations such as insertion and deletion are easier to implement in the doubly linked lists
since, unlike single linked lists, we do not need to traverse to the predecessor node and store its
reference. Rather, in a doubly linked list the reference of the predecessor node can be retrieved
from the node that we want to delete.
None
None e
e
Representation:
As always, let's first create a class for the single node in the list. Add the following code to your
file:
class Node:
def __init__(self, data):
self.item = data
self.nref = None
self.pref = None
Next, we need to create the DoublyLinkedList class, which contains different doubly linked list
related functions. Add the following code:
class DoublyLinkedList:
def __init__(self):
self.start_node = None
LAB TASK
Consider a scenario where a Pakistan Cricket Board (PCB) wants to maintain the data of its
international players. The data containing player shirt number, name, and batting average are
saved in a doubly linked list.
Shirt number
Prev Name Next
Batting average
POST LAB
Insert_mul: Insert multiple nodes at the end of the list. The number of nodes to be
inserted are passed as an argument and the data elements are taken as
input in the required function, insert_mul(numb)
Del_mult: a function to delete multiple nodes from the linked list. The function
accepts the position and the number of the nodes to be deleted as
arguments, Del_mult(pos,n). The function ist validates the position
and the number of the nodes. After validation, it deletes n nodes from
the given position.