0% found this document useful (0 votes)
6 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)
6 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
You are on page 1/ 6

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

self.data = data

self.next = None

class LinkedList:

def __init__(self):

self.head = None

def add(self, data):

new_node = Node(data)

new_node.next = self.head

self.head = new_node

def delete(self, key):

temp = self.head

if temp and temp.data == key:

self.head = temp.next

temp = None

return

prev = None

while temp and temp.data != key:


prev = temp

temp = temp.next

if temp is None:

return

prev.next = temp.next

temp = None

def display(self):

temp = self.head

while temp:

print(temp.data, end=" -> ")

temp = temp.next

print("None")

# Example Usage

ll = LinkedList()

ll.add(1)

ll.add(2)

ll.add(3)

ll.display()

ll.delete(2)

ll.display()

Output:

3 -> 2 -> 1 -> None

3 -> 1 -> None

You might also like