DSA Lab 6
DSA Lab 6
UNIVERSITY OF SARGODHA
Lab 6 Manual
Menu Driven Program for Doubly Linked List
Date Performed
Marks obtained
Instructor Signature
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 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 Doubly
Linked List (DLL)
a. Create a DLL by using end insertion.
b. Display the status of DLL and count the number of nodes in it
c. Perform Insertion and Deletion at End of DLL
d. Perform Insertion and Deletion at Front of DLL
e. Perform Insertion and Deletion after a given node DLL
f. Exit
In computer science, a doubly linked list is a linked data structure that consists of a set of sequentially linked
records called nodes. Each node contains two fields, called links, that are references to the previous and to
the next node in the sequence of nodes. The beginning and ending nodes' previous and next links,
respectively, point to some kind of terminator, typically a sentinel node or null, to facilitate traversal of the
list. If there is only one sentinel node, then the list is circularly linked via the sentinel node. It can be
conceptualized as two singly linked lists formed from the same data items, but in opposite sequential orders.
A doubly linked list whose nodes contain three fields: an integer value, the link to the next node, and the link
to the previous node.
The two node links allow traversal of the list in either direction. While adding or removing a node in a doubly
linked list requires changing more links than the same operations on a singly linked list, the operations are
simpler and potentially more efficient (for nodes other than first nodes) because there is no need to keep
track of the previous node during traversal or no need to traverse the list to find the previous node, so that its
link can be modified
ALGORITHM
Step 1: Start.
Step 2: Read the value of N. (N student’s information)
Step 3: Create a doubly linked list. (DLL)
Step 4: Display the status of DLL.
Step 5: Count the number of nodes.
Step 6: Perform insertion at front of list.
Step 7: Perform deletion at the front of the list.
Step 8: Perform insertion at end of the list.
Step 9: Perform deletion at the end of the list.
Step 10: Perform insertion after a given node of the list
Step 11: Perform deletion after a given node of the list.
Step 12: Stop
class DoublyLinkedList:
def __init__(self, n=None):
self.head = n
def printList(self):
if self.head is None:
print("No Nodes in Doubly Linked List")
else:
print("\nDoubly Linked List Values:")
temp = self.head
while temp is not None:
print(f"({temp.key}, {temp.data}) <--> ", end="")
temp = temp.next
print("")
if __name__ == "__main__":
obj = DoublyLinkedList()
while True:
print("\nWhat operation do you want to perform? Select Option number. Enter 0 to exit.")
print("1. appendNode()")
print("2. prependNode()")
print("3. insertNodeAfter()")
print("4. deleteNodeByKey()")
print("5. updateNodeByKey()")
print("6. print()")
print("7. Clear Screen\n")
option = int(input())
n1 = Node()
if option == 0:
break
elif option == 1:
print("Append Node Operation")
key1, data1 = map(int, input("Enter key & data of the Node to be Appended: ").split())
n1.key, n1.data = key1, data1
obj.appendNode(n1)
elif option == 2:
print("Prepend Node Operation")
key1, data1 = map(int, input("Enter key & data of the Node to be Prepended: ").split())
n1.key, n1.data = key1, data1
obj.prependNode(n1)
elif option == 3:
print("Insert Node After Operation")
k1 = int(input("Enter key of existing Node after which you want to Insert this New node: "))
key1, data1 = map(int, input("Enter key & data of the New Node: ").split())
n1.key, n1.data = key1, data1
obj.insertNodeAfter(k1, n1)
elif option == 4:
k1 = int(input("Delete Node By Key Operation - Enter key of the Node to be deleted: "))
obj.deleteNodeByKey(k1)
elif option == 5:
print("Update Node By Key Operation")
key1, data1 = map(int, input("Enter key & NEW data to be updated: ").split())
obj.updateNodeByKey(key1, data1)
elif option == 6:
obj.printList()
elif option == 7:
# Clear screen (not supported in Python, you can skip this option)
pass
else:
print("Enter Proper Option number")
OUTPUT:
What operation do you want to perform? Select Option number. Enter 0 to exit.
1. appendNode()
2. prependNode()
3. insertNodeAfter()
4. deleteNodeByKey()
5. updateNodeByKey()
6. print()
7. Clear Screen
1
Append Node Operation
Enter key & data of the Node to be Appended: 2 23
Node Appended as Head Node
What operation do you want to perform? Select Option number. Enter 0 to exit.
1. appendNode()
2. prependNode()
3. insertNodeAfter()
4. deleteNodeByKey()
5. updateNodeByKey()
6. print()
7. Clear Screen
2
Prepend Node Operation
Enter key & data of the Node to be Prepended: 3 56
Node Prepended
What operation do you want to perform? Select Option number. Enter 0 to exit.
1. appendNode()
2. prependNode()
3. insertNodeAfter()
4. deleteNodeByKey()
5. updateNodeByKey()
6. print()
7. Clear Screen
3
Insert Node After Operation
Enter key of existing Node after which you want to Insert this New node: 2
Enter key & data of the New Node: 23 6
Node Inserted at the END
What operation do you want to perform? Select Option number. Enter 0 to exit.
1. appendNode()
2. prependNode()
3. insertNodeAfter()
4. deleteNodeByKey()
5. updateNodeByKey()
6. print()
7. Clear Screen
5
Update Node By Key Operation
Enter key & NEW data to be updated: 3 56
Node Data Updated Successfully
What operation do you want to perform? Select Option number. Enter 0 to exit.
1. appendNode()
2. prependNode()
3. insertNodeAfter()
4. deleteNodeByKey()
5. updateNodeByKey()
6. print()
7. Clear Screen
What operation do you want to perform? Select Option number. Enter 0 to exit.
1. appendNode()
2. prependNode()
3. insertNodeAfter()
4. deleteNodeByKey()
5. updateNodeByKey()
6. print()
7. Clear Screen
4
Delete Node By Key Operation - Enter key of the Node to be deleted: 2
Node Deleted in Between
What operation do you want to perform? Select Option number. Enter 0 to exit.
1. appendNode()
2. prependNode()
3. insertNodeAfter()
4. deleteNodeByKey()
5. updateNodeByKey()
6. print()
7. Clear Screen
Outcomes Assessed:
CLO-1 Illustrate understanding of key concepts of Data structures and Algorithms using Dev
C++ 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