Lab 5
Lab 5
Objectives:
Equipment:
Personal Computer with Python (IDLE, Jupyter Notebook, or any Python IDE).
Introduction:
This lab session introduces an initial idea of dynamic data structures – linked lists. Students are required
to complete the programming during the lab and get it checked by the instructor. Besides, students should
also complete the lab assignments given at the end of this manual.
Linked List:
Linked list is one of the fundamental data structures, and can be used to implement other data structures.
It consists of a sequence of nodes, each containing arbitrary data fields and one or two links pointing to
the next and/or previous nodes. The principal benefit of a linked list over a conventional array is that the
order of the linked items may be different from the order that the data items are stored in memory or on
disk, allowing the list of items to be traversed in a different order. Besides a single node of the linked list
can have data of multiple data types, unlike arrays where whole array is defined for one data type only.
NULL
Task 1
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
1
Data Structures and Algorithms Lab Lab 5
self.head = new_node
def print_list(self):
temp = self.head
while temp:
print(temp.data, end=' -> ')
temp = temp.next
print('None')
Task 2
# Example usage
print(llist.search(5)) # Output: True
print(llist.search(7)) # Output: False
Node Insertion:
Consider a linked list made up of several nodes. If it is required to insert a new node after a specific node
in the linked list, that specific node should be first searched in the linked list. Following is a method to
add a new node after a specific node in the linked list:
Initialize a variable which stores the data of the node to be searched.
Traverse the linked list (p = p->next inside a loop) from start to NULL and compare data of every
node with the data to be searched (using an if condition).
When that specific node is found, use references (pointers) to help insert the new node
Initialize data to variables of the new node.
Check if the link is connected to all the nodes (including the new node)
The following diagram will help you understand how to implement this function:
Data =
6
Data Structures and Algorithms Lab Lab 5
In the above example, a new node with data = 6 is inserted after the node with data = 3. Following the
diagram above, once the node with data = 3 is found you need to change the references (pointers)
appropriately to insert the new node in this list. The *next pointer of node with data = 3 should be
modified to point to the new node and the *next pointer of the node with data = 6 should point to the next
node in the list. Display the modified linked list after insertion of the new node to check if the new data is
being displayed or not.
Task 3
llist.insert_after(5, 6)
llist.print_list()
Node Deletion:
If it is required to delete a particular node from the linked list, there can be the following three
possibilities:
Deleting node from front of the list
Deleting node from the end of the list
Deleting node from in between the list
Deleting node from front of the list: This function can be implemented just by changing the node to
which *start pointer points i.e. the *start pointer should not point to the first node rather it should point to
the node next to the first node.
Deleting node from the end of the list: This function is implemented by changing the *next pointer of
the second last node to NULL. In this way the last node will be excluded from the list.
Deleting node from in between: this function is quite similar to the “insert node in between” function.
First, the node with a specific data (the node that is to be deleted) is searched in the list. Once that node is
found, the reference (pointer) of the node previous to it should be modified to point to the node next to the
node to be deleted.
Task 4
3
Data Structures and Algorithms Lab Lab 5
llist.delete_node(5)
llist.print_list()
4
Data Structures and Algorithms Lab Lab 5
LAB ASSIGNMENTS
Modify the Node class to store student registration numbers (integer) and names (string).
Modify the insert_front() and print_list() methods accordingly.
Implement a method insert_back(data) that inserts a node at the end of the linked list.
Lab Report
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
5
Data Structures and Algorithms Lab Lab 5
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
6
Data Structures and Algorithms Lab Lab 5
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________