0% found this document useful (0 votes)
31 views8 pages

Data Structures Lab: Assignment #2 Name: Muhammad Umair ID: 66144 Questions

The document contains an assignment for a Data Structures Lab with four programming questions. Each question involves implementing algorithms for linked lists, stacks, sorting arrays, and searching in a matrix. Code solutions are provided for each question, demonstrating the required functionalities.

Uploaded by

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

Data Structures Lab: Assignment #2 Name: Muhammad Umair ID: 66144 Questions

The document contains an assignment for a Data Structures Lab with four programming questions. Each question involves implementing algorithms for linked lists, stacks, sorting arrays, and searching in a matrix. Code solutions are provided for each question, demonstrating the required functionalities.

Uploaded by

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

Data Structures Lab

ASSIGNMENT #2
Name: Muhammad Umair
ID : 66144

Questions:
QUESTION NO. 1: Write a Program to rearrange the linked list so that all even nodes appear before odd
nodes.

QUESTION NO. 2: Write a recursive function to sort a stack such that the smallest element is on top. You
may only use push and pop operations.

QUESTION NO. 3: Write a program to modify the merge sort algorithm so that it sorts an array of
integers in descending order.

QUESTION NO. 4: Write a program to search for a target value in a 2D matrix where each row and
column are sorted in ascending order.

Code:
Q1:
class Node:
def __init__(self, val):
self.data = val
self.next = None

def rearrange_even_odd(head):
if head is None:
return None

even_start = even_end = None


odd_start = odd_end = None
current = head

while current:
next_node = current.next
current.next = None

if current.data % 2 == 0:
if even_start is None:
even_start = even_end = current
else:
even_end.next = current
even_end = current
else:
if odd_start is None:
odd_start = odd_end = current
else:
odd_end.next = current
odd_end = current

current = next_node

if even_end:
even_end.next = odd_start
return even_start
else:
return odd_start

def print_list(head):
while head:
print(head.data, end=" -> ")
head = head.next
print("None")

head = Node(17)
head.next = Node(15)
head.next.next = Node(8)
head.next.next.next = Node(9)
head.next.next.next.next = Node(2)
head.next.next.next.next.next = Node(19)
head.next.next.next.next.next.next = Node(24)

new_head = rearrange_even_odd(head)
print_list(new_head)
Output:
Q2:
def insert_sorted(stack, element):
if not stack or stack[-1] <= element:
stack.append(element)
else:
top = stack.pop()
insert_sorted(stack, element)
stack.append(top)

def sort_stack(stack):
if stack:
top = stack.pop()
sort_stack(stack)
insert_sorted(stack, top)

stack = [30, -5, 18, 14, -3]


sort_stack(stack)
print("Sorted stack (top to bottom):", stack)

Output:
Q3:

def merge_sort_desc(arr):
if len(arr) > 1:
mid = len(arr) // 2

L = []
for i in range(mid):
L.append(arr[i])

R = []
for i in range(mid, len(arr)):
R.append(arr[i])

merge_sort_desc(L)
merge_sort_desc(R)

i = j = k = 0

while i < len(L) and j < len(R):


if L[i] > R[j]:
arr[k] = L[i]
i += 1
else:
arr[k] = R[j]
j += 1
k += 1

while i < len(L):


arr[k] = L[i]
i += 1
k += 1

while j < len(R):


arr[k] = R[j]
j += 1
k += 1

arr = [5, 3, 8, 6, 2,19, 7, 4, 1, 0]


merge_sort_desc(arr)
print("Sorted in descending order:", arr)
Output:
Q4:
def search_matrix(matrix, target):
if not matrix or not matrix[0]:
return False

rows = len(matrix)
cols = len(matrix[0])

r = 0
c = cols - 1

while r < rows and c >= 0:


value = matrix[r][c]
if value == target:
return True
elif value > target:
c -= 1
else:
r += 1

return False

matrix = [
[1, 4, 7, 11],
[2, 5, 8, 12],
[3, 6, 9, 16]
]

target = int(input("Enter target value: "))

if search_matrix(matrix, target):
print(f"Target {target} Found!")
else:
print("Not found.")

Output:

You might also like