You are on page 1of 9

COLLEGE OF ENGINEERING & TECHNOLOGY

UNIVERSITY OF SARGODHA

CE 416: Data Structure and Algorithms (Lab)

Lab 4 Manual
Menu Driven Program Handling Single Linked Lists

Instructor & Demonstrator: Engr. Nauman Ahmad Tariq

Student Name OBAID UR REHMAN

Roll No. ELEN51F20R010

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

a. Create a SLL by using front insertion.

b. Display the status of SLL and count the number of nodes in it

c. Perform Insertion and Deletion at End of SLL

d. Perform Insertion and Deletion at Front of SLL (Demonstration of STACK)

e. Perform Insertion and Deletion after a given node of SLL

f. Exit

ABOUT THE EXPERIMENT:

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.

Types of Linked List:


Singly Linked List: Singly linked lists contain nodes which have a data part as well as an address
part i.e. next, which points to the next node in sequence of nodes. The operations we can perform on
singly linked lists are insertion, deletion and traversal.

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...

Process finished with exit code 0


Assessment Rubric for Lab 4
Method of Evaluation: Lab report.

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.

Performance 5 Excellent 4 Good 3 Satisfactory 2-1 Needs Marks


Improvement
Realization of Fully understand & Close to fully Partially Unable to
Experiment able to illustrate understand & able understands & able understand & able
CLO-1 Singly Linked to illustrate Singly to illustrate Singly to illustrate Singly
Lists Linked Lists Linked Lists Linked Lists

Conducting Completely able to Close to Partially able to Unable to


Experiment successfully completely able to successfully successfully
CLO-2 design and compile successfully design and compile design and compile
C++ programs for design and compile C++ programs for C++ programs for
Singly Linked C++ programs for Singly Linked Singly Linked
Lists Singly Linked Lists Lists
Lists
Data Collection Completely Close to Partially Unable to
and Data Analysis understand & able completely understand & able understand & able
CLO-3 to demonstrate understand & able to demonstrate to demonstrate
syntax of Singly to demonstrate syntax of Singly syntax of Singly
Linked Lists syntax of Singly Linked Lists Linked Lists
Linked Lists

Individual/Team Completely able to Close to Partially able to Unable to execute


Work execute different completely able to execute different different programs
CLO-4 programs for execute different programs for for Singly Linked
Singly Linked programs for Singly Linked Lists
Lists Singly Linked Lists
Lists

Total

You might also like