0% found this document useful (0 votes)
40 views

DSA Lab 6

The document describes a Python program that implements a menu-driven doubly linked list. The program allows the user to perform operations like insertion, deletion, and updating of nodes in the doubly linked list. These operations include appending a node, prepending a node, inserting a node after a given node, deleting a node by key, and updating a node's data by key. The output shows sample runs where the user performs each operation and the doubly linked list is updated accordingly.

Uploaded by

obaid ur rehman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

DSA Lab 6

The document describes a Python program that implements a menu-driven doubly linked list. The program allows the user to perform operations like insertion, deletion, and updating of nodes in the doubly linked list. These operations include appending a node, prepending a node, inserting a node after a given node, deleting a node by key, and updating a node's data by key. The output shows sample runs where the user performs each operation and the doubly linked list is updated accordingly.

Uploaded by

obaid ur rehman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

COLLEGE OF ENGINEERING & TECHNOLOGY

UNIVERSITY OF SARGODHA

CE 416: Data Structure and Algorithms (Lab)

Lab 6 Manual
Menu Driven Program for Doubly Linked List

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

ABOUT THE EXPERIMENT:


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.

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

Python Program Code:


class Node:
def __init__(self, k=0, d=0):
self.key = k
self.data = d
self.next = None
self.previous = None

class DoublyLinkedList:
def __init__(self, n=None):
self.head = n

def nodeExists(self, k):


temp = None
ptr = self.head
while ptr is not None:
if ptr.key == k:
temp = ptr
ptr = ptr.next
return temp

def appendNode(self, n):


if self.nodeExists(n.key) is not None:
print(f"Node Already exists with key value: {n.key}. Append another node with a different Key
value")
else:
if self.head is None:
self.head = n
print("Node Appended as Head Node")
else:
ptr = self.head
while ptr.next is not None:
ptr = ptr.next
ptr.next = n
n.previous = ptr
print("Node Appended")

def prependNode(self, n):


if self.nodeExists(n.key) is not None:
print(f"Node Already exists with key value: {n.key}. Append another node with a different Key
value")
else:
if self.head is None:
self.head = n
print("Node Prepended as Head Node")
else:
self.head.previous = n
n.next = self.head
self.head = n
print("Node Prepended")

def insertNodeAfter(self, k, n):


ptr = self.nodeExists(k)
if ptr is None:
print(f"No node exists with key value: {k}")
else:
if self.nodeExists(n.key) is not None:
print(f"Node Already exists with key value: {n.key}. Append another node with a different Key
value")
else:
nextNode = ptr.next
if nextNode is None:
ptr.next = n
n.previous = ptr
print("Node Inserted at the END")
else:
n.next = nextNode
nextNode.previous = n
n.previous = ptr
ptr.next = n
print("Node Inserted in Between")

def deleteNodeByKey(self, k):


ptr = self.nodeExists(k)
if ptr is None:
print(f"No node exists with key value: {k}")
else:
if self.head.key == k:
self.head = self.head.next
print(f"Node UNLINKED with keys value: {k}")
else:
nextNode = ptr.next
prevNode = ptr.previous
if nextNode is None:
prevNode.next = None
print("Node Deleted at the END")
else:
prevNode.next = nextNode
nextNode.previous = prevNode
print("Node Deleted in Between")

def updateNodeByKey(self, k, d):


ptr = self.nodeExists(k)
if ptr is not None:
ptr.data = d
print("Node Data Updated Successfully")
else:
print(f"Node Doesn't exist with key value: {k}")

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

Doubly Linked List Values:


(3, 56) <--> (2, 23) <--> (23, 6) <-->

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

Doubly Linked List Values:


(3, 56) <--> (23, 6) <-->
Assessment Rubric for Lab 6
Method of Evaluation: Lab report.

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.

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 Doubly Linked to illustrate Doubly to illustrate Doubly to illustrate Doubly
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
Doubly Linked C++ programs for Doubly Linked Doubly Linked
Lists Doubly 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 Doubly to demonstrate syntax of Doubly syntax of Doubly
Linked Lists syntax of Doubly 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 Doubly Linked
Doubly Linked programs for Doubly Linked Lists
Lists Doubly Linked Lists
Lists

Total

You might also like