data dtructure and algo class 1v
data dtructure and algo class 1v
Q1. Define sparse matrix. Briefly explain representation of sparse matrix with
the help of link list and array.
A sparse matrix is a matrix in which the majority of its elements are zero. In other words, it
is a matrix where only a small number of elements have non-zero values. Storing sparse
matrices using traditional two-dimensional array structures can be inefficient in terms of both
space and time, as it would allocate space for all the elements (including zeros). Instead,
specialized data structures can be used to efficiently store and manipulate sparse matrices.
Sparse matrices can be represented using various methods, but two common representations
are through linked lists and arrays.
In the linked list representation, only the non-zero elements of the matrix are stored along
with their row and column indices. This approach reduces space complexity significantly.
Structure of a Node: Each node in the linked list can have the following structure:
Q2. Given a two dimensional array A1(1:8, 7:14) stored in row-major order with
base address 100 and size of each element is 4 bytes, find address of the element
A1(4, 12).
To find the address of the element ( A1(4, 12) ) in a two-dimensional array stored in
row-major order, we can use the following formula:
In this case:
Q3. Given a two dimensional array Z1(2:9, 9:18) stored in column-major order
with base address 100
and size of each element is 4 bytes, find address of the element Z1(4, 12).
To find the address of the element ( Z1(4, 12) ) in a two-dimensional array stored in
column-major order, we can use the following formula:
Given:
Q4. What is stack? Explain basic primitive operation of stack with example?
A stack is a linear data structure that follows the Last In, First Out (LIFO) principle,
meaning that the last element added to the stack is the first one to be removed. It’s analogous
to a stack of plates, where you can only add or remove the top plate.
Concepts of Stack
Q5. Write a C program to implement a stack with all necessary overflow and
underflow checks using array.
#include <stdio.h>
#include <stdlib.h>
struct Stack {
int top;
int arr[MAX];
};
int main() {
struct Stack stack;
initialize(&stack);
int choice, value;
while (1) {
printf("1. Push\n2. Pop\n3. Peek\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
push(&stack, value);
break;
case 2:
value = pop(&stack);
if (value != -1) {
printf("Popped value: %d\n", value);
}
break;
case 3:
value = peek(&stack);
if (value != -1) {
printf("Top value: %d\n", value);
}
break;
case 4:
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}
Q5. What is the advantage of Polish expression over infix notation? Write an
algorithm to convert an infix expression into reverse Polish expression
Polish notation, also known as prefix notation, and Reverse Polish Notation (RPN) (postfix
notation) have distinct advantages over traditional infix notation when it comes to evaluating
expressions.
1. Initialize an empty stack for operators and an empty list (or string) for the output (postfix
expression).
2. Scan each character in the infix expression from left to right:
o If the character is an operand (e.g., a variable or number), append it to the output
list.
o If the character is an operator (e.g., +, -, *, /):
▪ While there is an operator at the top of the stack with greater or equal
precedence than the current operator (and the stack is not empty), pop
operators from the stack to the output list.
▪ Push the current operator onto the stack.
o If the character is an opening parenthesis (, push it onto the stack.
o If the character is a closing parenthesis ):
▪ Pop operators from the stack to the output list until an opening parenthesis
( is encountered.
▪ Discard the opening parenthesis.
3. After the expression has been fully scanned, pop any remaining operators from the stack to
the output list.
4. The output list now contains the postfix (RPN) expression.
Q6. Convert the given infix expression to postfix expression.
1. Expression: A + ( (B – C) * (D – E) + F) / G $ (H – J)
• Postfix: A B C - D E - * F + G / H J - $ +
2. Expression: (A + B) * (C – D) $ E * F
• Postfix: A B + C D - E $ F * *
3. Expression: (A + B) * (C ^ (D – E) + F) – G
• Postfix: A B + C D E - ^ F + * G -
4. Expression: A + B * C
• Postfix: A B C * +
5. Expression: A + B * C ^ D – E
• Postfix: A B C D ^ * + E -
6. Expression: A + [(B + C) + (D + E) * F] / G
• Postfix: A B C + D E + F * + G / +
7. Expression: (A + B) * C / D + E ^ F / G
• Postfix: A B + C * D / E F ^ G / +
8. Expression: (A + B) * C / D
• Postfix: A B + C * D /
9. Expression: ((A + B – C / D) / E)
• Postfix: A B + C D / - E /
10. Expression: A / (B – C / D ^ E) + F
• Postfix: A B C D E ^ / - / F +
11. Expression: A – B / (C * D ^ E)
• Postfix: A B C D E ^ * / -
Result: 13
• 52=255^2 = 2552=25
• 2×25=502 \times 25 = 502×25=50
• 9/3=39 / 3 = 39/3=3
• 1×8=81 \times 8 = 81×8=8
• Now we have: 4+50+3−84 + 50 + 3 - 84+50+3−8
• 4+50=544 + 50 = 544+50=54
• 54+3=5754 + 3 = 5754+3=57
• 57−8=4957 - 8 = 4957−8=49
Result: 49
3) 9+5×7−62+9/39 + 5 \times 7 - 6^2 + 9 / 39+5×7−62+9/3
Result: 11
Result: 50
1. 12+: 1+2=3
• Stack: [3,3,−,2,1,+,3,−,+]
2. 3−: 3−3=0
• Stack: [0,2,1,+,3,−,+]
3. 21+: 2+1=3
• Stack: [0,3,3,−,+]
4. 31+: 3+1=4
• Stack: [5,4,∗]
5. 54∗: 5×4=20
Result: 20