[go: up one dir, main page]

0% found this document useful (0 votes)
122 views2 pages

Doubly Linked List in Python: Objective

This document discusses implementing a doubly linked list in Python. It begins by stating the objectives of understanding doubly linked lists and implementing one. It then describes that doubly linked lists allow traversal in both directions by having references to both the next and previous nodes. It also notes that basic operations like insertion and deletion are easier than in single linked lists as the predecessor node reference is directly available. It provides code to define a Node class with next and previous references and a DoublyLinkedList class to contain list functions. Finally, it provides a lab task to create functions for a doubly linked list to store Pakistan cricket players' data by shirt number, name, and batting average.

Uploaded by

ALvi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
122 views2 pages

Doubly Linked List in Python: Objective

This document discusses implementing a doubly linked list in Python. It begins by stating the objectives of understanding doubly linked lists and implementing one. It then describes that doubly linked lists allow traversal in both directions by having references to both the next and previous nodes. It also notes that basic operations like insertion and deletion are easier than in single linked lists as the predecessor node reference is directly available. It provides code to define a Node class with next and previous references and a DoublyLinkedList class to contain list functions. Finally, it provides a lab task to create functions for a doubly linked list to store Pakistan cricket players' data by shirt number, name, and batting average.

Uploaded by

ALvi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Doubly Linked List in Python

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

Create following functions for the players list.


InsertAthead: Insertion of a record at the head.
InsertAtEnd: Insertion of a record at the end.
Insert: Insertion of a record at any position in the list
Insert_before: Insertion of a record before the given shirt numb if present
Deletehead: Deletion of a record at the head
DeleteAtEnd: Deletion of a record at the end.
Delete_avg: Delete all the records having batting average equal to B
DisplayII: Displaying all records(starting from last to head ).

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.

You might also like