0% found this document useful (0 votes)
9 views6 pages

DSA Programs

Uploaded by

rushikhandare108
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)
9 views6 pages

DSA Programs

Uploaded by

rushikhandare108
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

Program 1

Aim: Write a program to find the sum and product of two matrices using the list data structure.

Code:

# Sum and Product of Two Matrices

def matrix_operations(matrix1, matrix2):

rows, cols = len(matrix1), len(matrix1[0])

# Sum of matrices

sum_matrix = [[matrix1[i][j] + matrix2[i][j] for j in range(cols)] for i in

range(rows)]

# Product of matrices

product_matrix = [[sum(matrix1[i][k] * matrix2[k][j] for k in range(cols)) for j in

range(cols)] for i in range(rows)]

return sum_matrix, product_matrix

# Example Matrices

matrix1 = [[1, 2], [3, 4]]

matrix2 = [[5, 6], [7, 8]]

# Operations

sum_matrix, product_matrix = matrix_operations(matrix1, matrix2)

print("Sum of matrices:", sum_matrix)

print("Product of matrices:", product_matrix)


Output:

Sum of matrices: [[6, 8], [10, 12]]

Product of matrices: [[19, 22], [43, 50]]


Program 2
Aim: Write a program to implement linked lists (single, doubly, and circular) with functions for adding, deleti

Code:

# Single Linked List Implementation

class Node:

def __init__(self, data):

[Link] = data

[Link] = None

class LinkedList:

def __init__(self):

[Link] = None

def add(self, data):

new_node = Node(data)

new_node.next = [Link]

[Link] = new_node

def delete(self, key):

temp = [Link]

if temp and [Link] == key:

[Link] = [Link]

temp = None

return

prev = None

while temp and [Link] != key:


prev = temp

temp = [Link]

if temp is None:

return

[Link] = [Link]

temp = None

def display(self):

temp = [Link]

while temp:

print([Link], end=" -> ")

temp = [Link]

print("None")

# Example Usage

ll = LinkedList()

[Link](1)

[Link](2)

[Link](3)

[Link]()

[Link](2)

[Link]()

Output:

3 -> 2 -> 1 -> None

3 -> 1 -> None

You might also like