Professional Documents
Culture Documents
LECTURE 3
Applications of stack
There are a number of applications of stacks such as;
3) To evaluate the arithmetic expressions such as, infix, prefix and postfix.
Precedence
Three Levels of Precedence for the usual five binary operations:
1. Infix Notation: the operator symbol is placed between its two operands:
Example: A+B, C-D, E*F, G/H
2. Prefix Notation: In which the operator symbol is placed before its two operands
Or Polish notation
Example: +AB,-CD,*EF,/GH
3. Postfix Notation: In which the operator symbol is placed after its two operands
Or Reverse Polish Notation
Example: AB+,CD-,EF*,GH/
Conversions
Conversions
CONVERSIONS
Infix Postfix
2-3*4+5 2-34*+5
234*-+5
234*-5+
Example:
• 3+4*5/6 (Infix)
345*6/+ (Postfix)
• (300+23)*(43-21)/(84+7) (Infix)
300 23 + 43 21 -* 84 7 + / (Postfix)
• (4+8)*(6-5)/((3-2)*(2+2)) (Infix)
4 8 + 6 5 -* 3 2 –2 2 + * / (Postfix)
Algorithm for Evaluation of a Postfix Expression
Algorithm : This algorithm finds the VALUE of an
arithmetic expression P written in postfix notation:
1. Add a right parenthesis “)” at the end of P. Example
2. Scan P from left to right and repeat steps 3 P: 5, 6, 2, +, *, 12, 4, /,-
and 4 for each element of P until the
sentinel ) is encountered. Symbol Scanned Stack
3. if an operand is encountered, then add to 5 5
stack:
4. if an operator (*) is encountered, then: 6 5,6
a) Remove the two top element of 2 5,6,2
STACK, where A is the top element + 5,8
and B is the next-to-top element.
b) Evaluate B(*)A. * 40
c) Place the result of (b) back on STACK. 12 40,12
End of IF structure. 4 40,12,4
End of Step 2 Loop.
5.Set VALUE equal to the top element on STACK. / 40,3
6. EXIT. - 37
)
623+-382/+*2^3+=52
Evaluation of a Postfix Notation
Evaluation of a Postfix Notation
Evaluation of a Postfix Notation
Evaluation of a Prefix Notation
Algorithm for Conversion from Infix to Postfix Expression
Algorithm: POLISH(Q,P)
Suppose Q is an arithmetic expression written in infix notation. This algorithm finds equivalent Postfix expression P.
1. Push “(“ onto STACK and add “)” to the end of Q.
2. Scan Q from left to right and repeat Steps 3 to 6 for each element of Q until the STACK is empty.
3. if an operand is encountered, add it to P.
4. if a left parenthesis is encountered, push it onto STACK.
5. if an operator (*) is encountered , then:
a) Repeatedly pop from STACK and add to P each operator(on the top of STACK) which has the same precedence
as or higher precedence then(*).
b) Add (*) to STACK.
END OF IF structure.
6. if a right parenthesis is encountered, then:
a) Repeatedly pop from STACK and add to P each operator (on the top of STACK) until a left parenthesis is
encountered.
b) Remove the left parenthesis. [Do not add the left parenthesis to P]
End of If Structure.
End of Step 2 Loop.
7. EXIT.
Transforming Infix into Postfix Expression
Symbol Scanned Stack Expression P
A ( A
+ (+ A
( (+( A Q: A+(B*C-(D/E Î F)*G)*H
B (+( AB
* (+(* AB
C (+(* ABC
- (+(- ABC*
( (+(-( ABC*
D (+(-( ABC*D
/ (+(-(/ ABC*D
E (+(-(/ ABC*DE
Î (+(-(/Î ABC*DE
F (+(-(/Î ABC*DEF
) (+(- ABC*DEFÎ/
* (+(-* ABC*DEFÎ/
G (+(-* ABC*DEFÎ/G
) ( + ABC*DEFÎ/G*-
* (+* ABC*DEFÎ/G*-
H (+* ABC*DEFÎ/G*-H
) ABC*DEFÎ/G*-H*+
Infix to Prefix Conversion Steps
Transforming Infix to Prefix Expression
Symbol Scanned Stack Expression P
(
5 ( 5
Q=(A+B^C)*D+E^5
^ (^ 5 Step 1: Reverse the infix
E (^ 5E expression
+ (+ 5E^
5^E+D*)C^B+A(
Step 2: Convert every ‘(‘ to ‘)’
D (+ 5E^D
And ‘)’..
* (+* 5E^D Now the expression:
( (+*( 5E^D Q: 5^E+D*(C^B+A))
Step 3:
C (+*( 5E^DC
Apply Algorithm for conversion
^ (+*(^ 5E^DC infix to postfix
B (+*(^ 5E^DCB Step 4:
+ (+*(+ 5E^DCB^
Reverse the Expression P
Answer:
A (+*(+ 5E^DCB^A
+*A^BCD^E5
) (+* 5E^DCB^A+
) 5E^DCB^A*+
Linear Search Algorithm
Linear Search
LINEAR(DATA,N,ITEM,LOC)
Here DATA is a linear array with N elements, and ITEM is a given item of information. This algorithm finds the location
LOC of ITEM in DATA, or sets LOC=0 if the search is unsuccessful.
1. [Insert ITEM at the end of DATA.] Set DATA[N+1]=ITEM.
2. [Initialize counter.] Set LOC=1
3. [Search for ITEM]
Repeat while DATA[LOC]= ITEM:
Set LOC=:LOC+1
[End of loop.]
4. [Successful?] if LOC=N+1, then : Set LOC=0
5. Exit.
Linear Search Program
Binary Search Algorithm
Binary Search:
BINARY(DATA,LB,UB,ITEM,LOC)
Here DATA is a sorted array with lower bound LB and upper bound UB, and ITEM is a given item of information. The variables
BEG, END and MID denote respectively, the beginning, end and the middle locations of a segment of elements of DATA. This
algorithm finds the location LOC of ITEM in DATA or sets LOC=NULL.
1. [Initialize segment variables.]
Set BEG=LB, END=UB and MID=INT(BEG+END)/2
2. Repeat steps 3 and 4 while BEG<=END and DATA[MID]=ITEM.
3. If ITEM < DATA[MID], then
Set END=MID-1
Else
Set BEG=MID+1
End of if structure.
4. Set MID=INT((BEG+END)/2)
End of step 2 loop.
5. If DATA[MID]=ITEM, then
Set LOC=MID
Else
Set LOC=NULL
End of IF structure
6. Exit.
Binary Search Program