You are on page 1of 2

CSC-18F-066 ABDUL MOMIN DATA STRUCTURE AND ALGORITHM

SECTION CS2C

Q: A +( B * C – ( D / E ↑ F ) * G ) * H

I). CONVERT INTO PREFIX NOTATION.

II). MAKE SYMBOL TABLE WHICH SHOW STATUS OF STACK

III). WRITE ALGORITHM

Ans I:

A +( B * C – ( D / E ↑ F ) * G ) * H

A+( B * C – ( D / ↑ EF ) * G ) * H

A+ ( B * C – ( /D ↑ EF ) * G ) * H

A + ( *BC - */D↑EFG) * H

A + ( -*BC */D↑EFG * H)

(A )+ ( * -*BC */D↑EFGH)

+ A *-* BC*/D↑EFGH

Ans II :

TABLE
A +( B * C – ( D / E ↑ F ) * G ) * H )

IN REVERSE: ( H * ( G * ( F ↑ E / D ) – C * B ) + A

S.R SCANNED SYMBOL STACK EXPRESSION


1 H ( H
2 * (* H
3 ( (*( H
4 G (*( HG
5 * (*(* HG
6 ( (*(*( HG
7 F (*(*( HGF
8 ↑ (*(*(↑ HGF
9 E (*(*(↑ HGFE
CSC-18F-066 ABDUL MOMIN DATA STRUCTURE AND ALGORITHM
SECTION CS2C

10 / (*(*(/ HGFE↑
11 D (*(*(/ HGFE↑D
12 ) (*(* HGFE↑D/
13 - (*(- HGFE↑D/*
14 C (*(- HGFE↑D/*C
15 * (*(-* HGFE↑D/*C
16 B (*(-* HGFE↑D/*CB
17 ) (* HGFE↑D/*CB*-
18 + (+ HGFE↑D/*CB*-*
19 A (+ HGFE↑D/*CB*-*A
20 EMPTY HGFE↑D/*CB*-*A+

AGAIN IN REVERSE WE GET: + A *-* BC*/D↑EFGH

ANS III :

Suppose Q is an arithmetic expression written in infix notation.


This algorithm finds the equivalent PREFIX expression P.
1. PUSH “(” onto STACK, and add “)” to the end of Q
2.Reverse the expression
3.Scan Q from left to right and repeat step 3 to step 6 for each element of Q until the STACK is
empty.
4. If an operand is encountered, add it to P
5. If a left parenthesis is encountered, push it onto STACK.
6. IF an operator X is encountered then:
i) Repeatedly POP from STACK and add to P each operator (on the top of STACK) which has the
same precedence as or higher precedence than X
ii) Add X to STACK [END of IF Structure]
7. IF a right parenthesis is encountered then:
i) Repeatedly POP from STACK and add to P each operator (on the top of STACK) until a left
parenthesis is encountered.
ii) Remove the left parenthesis. [Do not add the left parenthesis to P]
[End of IF Structure]
[End of STEP 2 loop]
8. Reverse the expression P and Exit

You might also like