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

Dsa Mod1 Finals

Uploaded by

Andrei Espadero
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)
15 views6 pages

Dsa Mod1 Finals

Uploaded by

Andrei Espadero
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
You are on page 1/ 6

ESPADERO, MAR ANDREI D.

BSIT-NET2A
Intended Learning Activities
1.Convert the following expression to postsfix : 10 + 3 * 5 / ( 16 - 4)
Sr. no. Expression Stack Postfix
0 (
1 1 ( 1
2 0 ( 10
3 ( 10
4 + (+ 10
5 (+ 10
6 3 (+ 10 3
7 (+ 10 3
8 * (+* 10 3
9 (+* 10 3
10 5 (+* 10 3 5
11 (+* 10 3 5
12 / (+/ 10 3 5 *
13 (+/ 10 3 5 *
14 1 (+/( 10 3 5 * 1
15 6 (+/( 10 3 5 * 16
16 (+/( 10 3 5 * 16
17 - (+/(- 10 3 5 * 16
18 (+/(- 10 3 5 * 16
19 4 (+/(- 10 3 5 * 16 4
20 ) (+/ 10 3 5 * 16 4-
21 ) 10 3 5 * 16 4 - / +

2. Convert the following expression to prefix and postfix:


2.1 A+B Postfix:
Sr. no. Expression Stack Prefix Prefix: Sr. no. Expression Stack Postfix
0 ( 0 (
1 B ( B 1 A ( A
2 + (+ B 2 + (+ A
3 A (+ BA 3 B (+ AB
4 ) BA+ 4 (+ AB
5 (+ AB
6 ) AB +
2.2 A*B+ C
Prefix: Postfix:
Sr. no. Expression Stack Postfix
Sr. no. Expression Stack Prefix
0 (
0 (
1 A ( A
1 C ( C
2 * (* A
2 ( C
3 B (* AB
3 + (+ C
4 + (+ AB*
4 (+ C
5 (+ AB*
5 B (+ CB
6 C (+ AB* C
6 (+ CB
7 ) A B * C+
7 * (+* CB
8 (+* CB
9 A (+* CBA
10 ) C B A* +

2.3 (A*B) + ( C*D)


Prefix: Postfix:
Sr. no. Expression Stack Prefix Sr. no. Expression Stack Postfix
0 ( 0 (
1 D (( D 1 A (( A
2 * ((* D 2 * ((* A
3 C ((* DC 3 B ((* AB
4 ) ( DC* 4 ) ( AB*
5 ( DC* 5 ( AB*
6 + (+ DC* 6 + (+ AB*
7 (+ DC* 7 (+ AB*
8 B (+( DC* B 8 C (+( AB* C
9 * (+(* DC* B 9 * (+(* AB* C
10 A (+(* DC* BA 10 D (+(* AB* CD
11 ) (+ DC* BA* 11 ) (+ AB* CD*
12 ) DC* BA*+ 12 ) AB* CD*+
Assignment
1. List down the different stack operations.
- The basic operations of Stacks are Push and Pop
Push operation
- The Process of Push Operation is adding a new data element to the stack.
Pop operation
- The Process of Pop Operation is removing the last data element added to the stack.

2. Differentiate the 3 stack notations and by giving at least 3 examples in each notation.

1. Infix notation
- Infix notation is used to write expressions where operators are used in-between operands.

Sr.no. Infix notation


1 x-y
2 (x / y) * z
3 (x * y) + z
2. Prefix notation
- Prefix notation is used to write expressions where operators are used in is written in advance operands.

Sr.no. Prefix notation


1 - xy
2 */x y z
3 +*x y z

3. Postfix notation
- Postfix notation is used to write expressions operators are used in is written in afterwards operands.

Sr.no Postfix notation


.
1 xy -
2 xy / z *
3 x y* z+
Assessment
1. Debug & Run the following program. Show the output.
import java.util.Stack;

class Test1 {
int val1 = stack.pop();
static int evaluatePostfix(String exp){ int val2 = stack.pop();

switch(c)
Stack<Integer> stack = new Stack<>();
{

for(int i = 0; i < exp.length(); i++) case '+':


{ stack.push(val2+val1);
break;
char c = exp.charAt(i);
case '-':
if(c == ' ') stack.push(val2- val1);
break;
continue;
case '/':
stack.push(val2/val1);
break;
else if(Character.isDigit(c))
{ case '*':
stack.push(val2*val1);
int n = 0; break;

while(Character.isDigit(c)) }
}
{ }

n = n*10 + (int)(c -'0'); return stack.pop();

i++; }

c = exp.charAt(i);
public static void main(String[] args){
}
String exp = "100 200 + 2 / 5 * 7 +";
i --;
System.out.println(evaluatePostfix(exp));

stack.push(n); }
} }

else
{
Output:
757

You might also like