0% found this document useful (0 votes)
6 views7 pages

Lab 5

This lab focuses on implementing and manipulating singly linked lists using Python. Students will learn to perform operations such as insertion, deletion, and searching within the linked list. The lab includes practical tasks and assignments to reinforce the concepts learned.

Uploaded by

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

Lab 5

This lab focuses on implementing and manipulating singly linked lists using Python. Students will learn to perform operations such as insertion, deletion, and searching within the linked list. The lab includes practical tasks and assignments to reinforce the concepts learned.

Uploaded by

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

Data Structures and Algorithms Lab Lab 5

INTRODUCTION TO LINKED LISTS

Objectives:

 To learn how to implement a singly linked list.


 To learn how to perform different operations like inserting, printing, searching, and deleting
within a linked list.

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.

Consider the following diagrammatic representation for a linked list.

NULL

Task 1

Linked List Implementation in Python

class Node:
def __init__(self, data):
self.data = data
self.next = None

class LinkedList:
def __init__(self):
self.head = None

def insert_front(self, data):


new_node = Node(data)
new_node.next = self.head

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')

# Creating linked list and inserting nodes


llist = LinkedList()
llist.insert_front(3)
llist.insert_front(5)
llist.insert_front(1)
llist.print_list()

Task 2

Searching a Node in a Linked List

def search(self, key):


temp = self.head
while temp:
if temp.data == key:
return True
temp = temp.next
return False

# 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 = Data = Data = NULL


1 3 5
2

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

Inserting a Node at a Specific Position

def insert_after(self, prev_data, new_data):


temp = self.head
while temp and temp.data != prev_data:
temp = temp.next
if temp:
new_node = Node(new_data)
new_node.next = temp.next
temp.next = new_node

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

Deleting a Node from a Linked List

def delete_node(self, key):


temp = self.head

3
Data Structures and Algorithms Lab Lab 5

if temp and temp.data == key:


self.head = temp.next
return
prev = None
while temp and temp.data != key:
prev = temp
temp = temp.next
if temp:
prev.next = temp.next

llist.delete_node(5)
llist.print_list()

4
Data Structures and Algorithms Lab Lab 5

LAB ASSIGNMENTS

Q1: Implement a Linked List with Basic Operations

 Create a linked list with 5 nodes using the insert_front() method.


 Use the print_list() method to display the linked list.

Q2: Modify the Linked List to Store Student Data

 Modify the Node class to store student registration numbers (integer) and names (string).
 Modify the insert_front() and print_list() methods accordingly.

Q3: Search for a Student in the Linked List

 Implement a method search_student(reg_number) to find a student using their registration


number.

Q4: Implement Node Insertion at the End

 Implement a method insert_back(data) that inserts a node at the end of the linked list.

Q5: Implement Node Insertion and Deletion in Between

 Implement a method insert_after(data, new_data) to insert a node after a specific node.


 Implement a method delete_node(data) to delete a node with a specific value.

Q6: Parking Lot System Using Linked List

 Create a class Car to store car number plates.


 Implement methods to add and remove cars from a linked list.
 Implement a method to check if the parking lot is empty.
 Implement a method to search for a car in the parking lot.
 Maintain a count of the number of cars in the parking lot.

Lab Report

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

5
Data Structures and Algorithms Lab Lab 5

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

6
Data Structures and Algorithms Lab Lab 5

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________

You might also like