Professional Documents
Culture Documents
UNIVERSITY OF SARGODHA
Lab 4 Manual
Menu Driven Program Handling Single Linked Lists
Date Performed
Marks obtained
Instructor Signature
CLO-1 Illustrate understanding of key concepts of Data structures and
Algorithms using Python Pucharm Platform
CLO-2 Design a programming application by applying concepts of Data
Structures and algorithms leading to the solution of a moderate scale-
programming problem.
CLO-3 Write lab notes, effective communication and the analysis of the
given problem to perform in the laboratory environment.
CLO-4 Demonstrate involvement in the Project as a team or individually
with respect to the contribution.
OBJECTIVE:
Design, Develop and Implement a menu driven Program in Python for the following operations on Singly
Linked List (SLL).
f. Exit
Linked List is a linear data structure and it is very common data structure which consists of group of
nodes in a sequence which is divided in two parts. Each node consists of its own data and the address
of the next node and forms a chain. Linked Lists are used to create trees and graphs.
They are a dynamic in nature which allocates the memory when required.
Insertion and deletion operations can be easily implemented.
Stacks and queues can be easily executed.
Linked List reduces the access time.
Linked lists are used to implement stacks, queues, graphs, etc.
Linked lists let you insert elements at the beginning and end of the list.
In Linked Lists we don’t need to know the size in advance.
Doubly Linked List: In a doubly linked list, each node contains two links the first link points to the
previous node and the next link points to the next node in the sequence.
Circular Linked List: In the circular linked list the last node of the list contains the address of
the first node and forms a circular chain.
ALGORITHM:
Step 1: Start.
Step 2: Read the value of N. (N student’s information)
Step 2: Create a singly linked list. (SLL)
Step 3: Display the status of SLL.
Step 4: Count the number of nodes.
Step 5: Perform insertion at front of list.
Step 6: Perform deletion at the front of the list.
Step 7: Perform insertion at end of the list.
Step 8: Perform deletion at the end of the list.
Step 9: Stop
Python Program Code:
class Node:
def __init__(self, value):
self.info = value
self.next = None
class SingleLinkedList:
def __init__(self):
self.start = None
def insert_begin(self):
value = int(input("Enter the value to be inserted: "))
temp = Node(value)
if self.start is None:
self.start = temp
self.start.next = None
else:
temp.next = self.start
self.start = temp
print("Element Inserted at the beginning")
def insert_last(self):
value = int(input("Enter the value to be inserted: "))
temp = Node(value)
if self.start is None:
self.start = temp
else:
s = self.start
while s.next is not None:
s = s.next
s.next = temp
print("Element Inserted at last")
def insert_pos(self):
value = int(input("Enter the value to be inserted: "))
pos = int(input("Enter the position at which the node should be inserted: "))
temp = Node(value)
counter = 0
s = self.start
while s is not None:
s = s.next
counter += 1
if pos == 1:
if self.start is None:
self.start = temp
else:
temp.next = self.start
self.start = temp
elif 1 < pos <= counter:
s = self.start
for i in range(1, pos):
ptr = s
s = s.next
ptr.next = temp
temp.next = s
else:
print("Position out of range")
def delete_pos(self):
pos = int(input("Enter the position of the value to be deleted: "))
counter = 0
s = self.start
if self.start is None:
print("List is empty")
return
if pos == 1:
self.start = s.next
else:
while s is not None:
s = s.next
counter += 1
if 1 <= pos <= counter:
s = self.start
for i in range(1, pos):
ptr = s
s = s.next
ptr.next = s.next
else:
print("Position out of range")
return
print("Element Deleted")
def display(self):
temp = self.start
if temp is None:
print("The List is Empty")
return
print("Elements of list are:")
while temp is not None:
print(temp.info, end=" -> ")
temp = temp.next
print("None")
if __name__ == "__main__":
sll = SingleLinkedList()
while True:
print("\n---------------------------------")
print("Operations on singly linked list")
print("---------------------------------")
print("1. Insert Node at beginning")
print("2. Insert node at last")
print("3. Insert node at position")
print("4. Delete a Particular Node")
print("5. Display Linked List")
print("6. Exit")
choice = int(input("Enter your choice : "))
if choice == 1:
print("Inserting Node at Beginning:")
sll.insert_begin()
elif choice == 2:
print("Inserting Node at Last:")
sll.insert_last()
elif choice == 3:
print("Inserting Node at a given position:")
sll.insert_pos()
elif choice == 4:
print("Delete a particular node:")
sll.delete_pos()
elif choice == 5:
print("Display elements of the linked list")
sll.display()
elif choice == 6:
print("Exiting...")
break
else:
print("Wrong choice")
OUTPUT:
---------------------------------
Operations on singly linked list
---------------------------------
1. Insert Node at beginning
2. Insert node at last
3. Insert node at position
4. Delete a Particular Node
5. Display Linked List
6. Exit
Enter your choice : 1
Inserting Node at Beginning:
Enter the value to be inserted: 23
Element Inserted at the beginning
---------------------------------
Operations on singly linked list
---------------------------------
1. Insert Node at beginning
2. Insert node at last
3. Insert node at position
4. Delete a Particular Node
5. Display Linked List
6. Exit
Enter your choice : 2
Inserting Node at Last:
Enter the value to be inserted: 45
Element Inserted at last
---------------------------------
Operations on singly linked list
---------------------------------
1. Insert Node at beginning
2. Insert node at last
3. Insert node at position
4. Delete a Particular Node
5. Display Linked List
6. Exit
Enter your choice : 3
Inserting Node at a given position:
Enter the value to be inserted: 12
Enter the position at which the node should be inserted: 3
Position out of range
---------------------------------
Operations on singly linked list
---------------------------------
1. Insert Node at beginning
2. Insert node at last
3. Insert node at position
4. Delete a Particular Node
5. Display Linked List
6. Exit
Enter your choice : 5
Display elements of the linked list
Elements of list are:
23 -> 45 -> None
---------------------------------
Operations on singly linked list
---------------------------------
1. Insert Node at beginning
2. Insert node at last
3. Insert node at position
4. Delete a Particular Node
5. Display Linked List
6. Exit
Enter your choice : 4
Delete a particular node:
Enter the position of the value to be deleted: 3
Position out of range
---------------------------------
Operations on singly linked list
---------------------------------
1. Insert Node at beginning
2. Insert node at last
3. Insert node at position
4. Delete a Particular Node
5. Display Linked List
6. Exit
Enter your choice : 6
Exiting...
Outcomes Assessed:
CLO-1 Illustrate understanding of key concepts of Data structures and Algorithms using
Python Pycharm Platform.
CLO-2 Design a programming application by applying concepts of Data Structures and
algorithms leading to the solution of a moderate scale-programming problem.
CLO-3 Write lab notes, effective communication and analysis of the given problem to perform
in the laboratory environment.
CLO-4 Demonstrate involvement in the Project as a team or individually with respect to the
contribution.
Total