Download presentation
Presentation is loading. Please wait.
1
STACK IMPLEMENTATION Adam M.B.
2
NUMERIC AND POLISH NOTATION
NUMERIC AND POLISH NOTATION
3
Numeric Notation Infix Operator between operands Prefix
Operator before operands Postfix Operator after operands
4
Polish Notation Known as prefix notation (Jan Lukasiewicz). Example:
INFIX PREFIX A+B +AB A+B-C -+ABC (A+B)*(C-D) *+AB-CD
5
Postfix Notation (Suffix)
Known as Reverse Polish Notation (RPN). Example: INFIX POSTFIX A+B AB+ A+B-C AB+C- (A+B)*(C-D) AB+CD-*
6
INFIX TO POSTFIX
7
Processes Supposed Algorithm Q = Infix notation P = Postfix Notation
1 stack for temporary variable. Algorithm Push “(“ into stack and add “)” to sentinel of Q.
8
Processes Scan Q from left to right then repeat step c until f for each Q elements until Q is empty. If scan result is operand then add it to P. If scan result is “(“ then push to stack. If scan result is “)” then pop element of stack and add to P until found “(“. “(“ is not included in P.
9
Processes If scan result is operator then:
If top element of stack is operator that have higher or equal than the scanned operator then pop the operator in stack to P. For the contrary, push the scanned operator to stack.
10
Example 1 E = A + B Q : P : A + B ) AB+ ( 1. A ( A 2. + ( + A 3. B ( +
Num Symbol Stack P Expression ( 1. A ( A 2. + ( + A 3. B ( + A B 4. ) A B +
11
Example 2 E = A + (B – C) / D Q : P : A + (B – C) / D ) ABC-D/+ Num
Symbol Stack P Expression ( 1. A ( A (+ A 3. ( (+( A 4. B (+( AB (+(- AB 6. C (+(- ABC 7. ) (+ ABC- 8. / (+/ ABC- 9. D (+/ ABC-D 10. ) ABC-D/+
12
Example 3 In Q, there are 20 elements: Q :
A + ( B * C - ( D / E ^ F ) * G ) * H Q : A + ( B * C - ( D / E ^ F ) * G ) * H In Q, there are 20 elements: Q : ) A + ( B * C - D / E ^ F ) G H 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
13
Example 3 Num Symbol Stack P Expression ( 1 A 2 + (+ 3 (+( 4 B AB 5 *
(+(* 6 C ABC 7 - (+(- ABC* 8 (+(-( 9 D ABC*D 10 / (+(-(/
14
Example 3 Jadi P : ABC*DEF^/G*-H*+ Num Symbol Stack P Expression 11 E
(+(-(/ ABC*DE 12 ^ (+(-(/^ 13 F ABC*DEF 14 ) (+(- ABC*DEF^/ 15 * (+(-* 16 G ABC*DEF^/G 17 (+ ABC*DEF^/G*- 18 (+* 19 H ABC*DEF^/G*-H 20 ABC*DEF^/G*-H*+ Jadi P : ABC*DEF^/G*-H*+
15
Manual Way Using [ ] and its format [operand1,operand2,operator] and still concern about operator priority.
16
Example Q = A + B – C = [AB+] – C P = AB+C- b. Q = A + (B – C) / D
P = ABC-D/+
17
Exercise E = A + BD – F GH K b. E = A + BDH – F G - K
Convert infix notation into postfix notation with algoritma way and manual way
18
COUNTING IN POSTFIX
19
Processes Add “)” to sentinel of P.
Scan P from left to right. Repeat step C until d for each elements in P until sentinel is found. If scan result is operand then push it to stack.
20
Processes If scan result is operator (for example opr1) then:
Pop from stack then save it into var1 Pop again from stack then save it into var2 Count by format var2 opr1 var1 then save it into hitung variable. Push value of hitung variable into stack. If scan result is “)” then pop element of stack and save it into Value variable.
21
Example P : P : 2,6,3,-,1,/,+ ABC-D/+ Ex. A=2, B=6, C=3, D=1 ,)
Value = 5 Num Symbol Stack Counting Process 2 2,6 2,6,3 2,3 Var1=3,Var2=6,Hitung=Var2 Opr1 Var1 = = 3 2,3,1 6. / 2,3 Var1=1,Var2=3,Hitung=Var2 Opr1 Var1 = / = 3 5 Var1=3,Var2=2,Hitung=Var2 Opr1 Var1 = = 5 8. )
22
Manual Way Using [ ] by searching first operator from left then count with two left operands using format [operand1 operator operand2] .
23
Example P : 2,6,3,-,1,/,+ : 2,[6-3],1,/,+ : 2,3,1,/,+ : 2,[3/1],+
: 2,3,+ : [2+3] : 5
24
Exercise Do the task 6.31 (page 210); 6.32, (page 211) at Data Structures book, seymour. Make a program to convert infix notation into postfix operator and counting in postfix notation.
25
GRACIAS THANK YOU Copyright © Adam Mukharil Bachtiar 2012
Contact Person: Adam Mukharil Bachtiar Informatics Engineering UNIKOM Jalan Dipati Ukur Nomor Bandung 40132 Blog: Copyright © Adam Mukharil Bachtiar 2012
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.