0% found this document useful (0 votes)
13 views5 pages

DSA Assignment 2 - 20241031 - 021051 - 0000

The document contains two programming assignments related to linked lists in Python. The first assignment involves creating two linked lists, finding their intersection, and displaying the result. The second assignment requires creating two linked lists, finding their union, and displaying the combined list.

Uploaded by

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

DSA Assignment 2 - 20241031 - 021051 - 0000

The document contains two programming assignments related to linked lists in Python. The first assignment involves creating two linked lists, finding their intersection, and displaying the result. The second assignment requires creating two linked lists, finding their union, and displaying the combined list.

Uploaded by

mg2541568
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

TotalMarks: 04

Obtained Marks:

Data Structure & Algorithms

Assignment # 02

Submitted To: Mr. Muneeb Muzammil

Student Name: Bushra Batool

Reg. Number: 2312114


Qno1: Write a program in two create two linkedlist with user defined values
. Find the intersection of two lists in the third list.
Sample input: List 1: 1- >2->3->4->5
List 2: 7->9->3->2->6->4
Sample output: After Intersection: List 3: 3->2->4

class Node:
def __init__(self, data):
self.data = data self.next = None
class LinkedList: def __init__(self):
self.head = None
def insert self, data):
new_node = Node(data)
new_node.next = self.head
self.head = new_node
def display(self):
current = self.head while current:
print(current.data, end="->" if current.next else "\n")
current = current.next def to_set(self):
current, elements = self.head, set()
while current: elements.add(current.data)
current = current.next return elements
def intersect(list1, list2):
result = LinkedList()
for data in list1.to_set().intersection(list2.to_set()):
result.insert(data) return result # Sample input
list1, list2 = LinkedList(), LinkedList()
for value in [1, 2, 3, 24, 5]: list1.insert(value)
for value in [7, 29, 3, 2, 6, 4]: list2.insert(value)
# Find intersection and display
print("List 1:", end=" ") list1.display()
print("List 2:", end=" ") list2.display()
print("After Intersection: List 3:", end=" ")
intersect(list1, list2).display()
1 Output:

List 1: 1->2->3->24->5
List 2: 7->29->3->2->6->4
After Intersection: List 3: 3->2
Qno2: Write a program in two create two linkedlist with user defined
values. Find the unioin of two lists in the third list. Sample input: List 1: 1-
>2->3->4->5 List 2: 7->9->3->2->6 Sample output: After Union: List 3:
1- >2->3->4->5->7->9->6

class Node:
def __init__(self, data):
self.data = data
self.next = None class LinkedList:
def __init__(self):
self.head = None def insert(self, data):
new_node = Node(data)
new_node.next = self.head
self.head = new_node def to_set(self):
current, elements = self.head, set() while current:
elements.add(current.data)
current = current.next return elements
def union(list1, list2): result = LinkedList()
for data in list1.to_set() | list2.to_set():
result.insert(data) return result
def display_list(head): current = head while current:
print(current.data, end="->" if current.next else "\n") current =
current.next
# Create lists with sample values
list1, list2 = LinkedList(), LinkedList()
for value in [1, 2, 3, 4, 5]: list1.insert(value)
for value in [7, 9, 3, 2, 6]: list2.insert(value)
# Union and display
print("List 1:", end=" "); display_list(list1.head)
print("List 2:", end=" "); display_list(list2.head)
print("After Union: List 3:", end=" "); display_list(union(list1, list2).head)
2 Output:

List 1: 1->2->3->4->5
List 2: 7->9->3->2->6
After Union: List 3: 1->2->3->4->5->7->9->6

You might also like