DS - and - Algo - Assigment 2

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

ASSIGNMENT ANSWER

SHEET
NAME: DIVEK S
COURSE: DATA STRUCTURES
WITH ALGORITHMS
ASSIGNMENT 2
Undertaking:

I affirm that I have not given or received any unauthorized help on this assignment, and that this
work is my own. I understand that any violation of this will result in disciplinary action from the JAIN
Online program.

Answers

1. Write a program in java to implement the linear search technique.

import java.util.*;
import java.util.Scanner;

class LinearSearch {
static ArrayList<Integer> linearSearch(int a[], int val) {
ArrayList<Integer> indexes = new ArrayList<Integer>();

for (int i = 0; i < a.length; i++) {


if (a[i] == val)
indexes.add(i);
}
return indexes;
}

public static void main(String args[]) {


Scanner scanner = new Scanner(System.in);
System.out.println("Enter the length of the array");
int length = scanner.nextInt();
int[] arr = new int[length];
System.out.println("Enter the element of the array");
for (int i = 0; i < length; i++) {
arr[i] = scanner.nextInt();
}
System.out.println("Enter the key value");
int search = scanner.nextInt();
ArrayList<Integer> indexes = linearSearch(arr, search);
if (indexes.size() == 0) {
System.out.println("Element not found");
} else {
System.out.print("index : ");
for (int i = 0; i < indexes.size(); i++) {
System.out.print(indexes.get(i) + " ");
}
}
}
}
2. Solve the following questions:
a. Solve the following postfix expressions
i. 2 3 + 1 –
1. first number is 2, which is an operand, so push it to the stack. [ 2 ]
2. next number is 3, which is an operand, so push it to the stack. [ 3, 2]
3. next character is +, now pop 3 and 2 from stack and evaluate exp
2+3 = 5 and push 5 to stack [5]
4. next number is 1, which is an operand, so push it to the stack [1, 5]
5. next character is -, now pop 5 and 1 from stack and evaluate exp 5 -
1 = 4 and push 4 to stack [4]
6. result is 4, since all characters are done, the left-over element in
stack is 4

ii. 3 4 - 10 + 7 2 3 * - 9 * / (applying same as answered in (i) as above to


evaluate, only showing stack and expression)
1. [ 3]
2. [4, 3]
3. - so, 3 - 4 = -1 [-1]
4. [10, -1]
5. + so, -1 + 10 = 9 [9]
6. [ 7, 9]
7. [ 2, 7, 9]
8. [ 3, 2, 7, 9]
9. * so, 2* 3 = 6 [ 6, 7, 9]
10. – so, 7 – 6 = 1 [1, 9]
11. [9, 1, 9]
12. * so, 1 * 9 = 9 [9, 9]
13. / so, 9 / 9 =1 [1]
14. result is 1, since all characters are done, the left-over element in
stack is 1

b. Convert the following infix to postfix expressions


i. 2 * 3 - 5 / 2 + 4
1. 2 is an operand, append it to the postfix expression stack [], postfix:
2
2. * Is an operator and the stack is empty, push the * to the stack [ *],
postfix: 2
3. 3 is an operand, append it to the postfix expression stack [ *],
postfix: 2 3
4. - is an operator and has lower precedence than the * on the top of
the stack, pop * from the stack and append it to the postfix
expression stack [-], postfix: 2 3 *
5. 5 is an operand, append it to the postfix expression [-], postfix: 2 3 *
5
6. Continuing the to end we get postfix result as 2 3 * 5 2 / - 4 +

ii. 1 * 2 * 3 * 4
1. postfix result is 1 2 * 3 * 4 *
3. Overflow and underflow
a. overflow: This happens when a queue is full and there is no more space to put
element inside the queue, to avoid this, we need to check if rear is equal to size of
the queue, condition to be used is if (front == size – 1)// counting from 0
b. underflow: This happens when and queue is empty and we are trying to pop out
from queue, to avoid this we can check if front is equal to rear, condition if (front ==
rear)

You might also like