STACK IMPLEMENTATION Adam M.B.
NUMERIC AND POLISH NOTATION NUMERIC AND POLISH NOTATION
Numeric Notation Infix Operator between operands Prefix Operator before operands Postfix Operator after operands
Polish Notation Known as prefix notation (Jan Lukasiewicz). Example: INFIX PREFIX A+B +AB A+B-C -+ABC (A+B)*(C-D) *+AB-CD
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-*
INFIX TO POSTFIX
Processes Supposed Algorithm Q = Infix notation P = Postfix Notation 1 stack for temporary variable. Algorithm Push “(“ into stack and add “)” to sentinel of Q.
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.
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.
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 +
Example 2 E = A + (B – C) / D Q : P : A + (B – C) / D ) ABC-D/+ Num Symbol Stack P Expression ( 1. A ( A 2. + (+ A 3. ( (+( A 4. B (+( AB 5. - (+(- AB 6. C (+(- ABC 7. ) (+ ABC- 8. / (+/ ABC- 9. D (+/ ABC-D 10. ) ABC-D/+
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
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 / (+(-(/
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*+
Manual Way Using [ ] and its format [operand1,operand2,operator] and still concern about operator priority.
Example Q = A + B – C = [AB+] – C P = AB+C- b. Q = A + (B – C) / D P = ABC-D/+
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
COUNTING IN POSTFIX
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.
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.
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 1. 2 2 2. 6 2,6 3. 3 2,6,3 4. - 2,3 Var1=3,Var2=6,Hitung=Var2 Opr1 Var1 = 6 - 3 = 3 5. 1 2,3,1 6. / 2,3 Var1=1,Var2=3,Hitung=Var2 Opr1 Var1 = 3 / 1 = 3 7. + 5 Var1=3,Var2=2,Hitung=Var2 Opr1 Var1 = 2 + 3 = 5 8. )
Manual Way Using [ ] by searching first operator from left then count with two left operands using format [operand1 operator operand2] .
Example P : 2,6,3,-,1,/,+ : 2,[6-3],1,/,+ : 2,3,1,/,+ : 2,[3/1],+ : 2,3,+ : [2+3] : 5
Exercise Do the task 6.31 (page 210); 6.32, 6.35-6.37 (page 211) at Data Structures book, seymour. Make a program to convert infix notation into postfix operator and counting in postfix notation.
GRACIAS THANK YOU Copyright © Adam Mukharil Bachtiar 2012 Contact Person: Adam Mukharil Bachtiar Informatics Engineering UNIKOM Jalan Dipati Ukur Nomor. 112-114 Bandung 40132 Email: adfbipotter@gmail.com Blog: http://adfbipotter.wordpress.com Copyright © Adam Mukharil Bachtiar 2012