Data Structures
Stack – Application
Conversion -Infix to Prefix
Data Structure –Stack Application
Infix to Prefix
●
Operator
Infix Prefix Result
Conversion Evaluation ●
()
●
*/
●
2*3-3+8/4/(1+1) ●
+-
Data Structure –Stack Application
Infix to Prefix
●
Operator
Infix Prefix Result
Conversion Evaluation ●
()
●
*/
●
2*3-3+8/4/(1+1) ●
+-
●
2 * 3 – 3 + 8 / 4 / {+11}
●
{*23} -3 + {/84} / { +11}
●
{*23} -3 + {/ /84 +11}
●
{- *23 3} + {/ /84 +11}
●
+ - *233 / /84 +11 Prefix
Data Structure –Stack Application
Infix to Prefix
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand)
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) )
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) )
Prefix array = pop
Discard ‘(‘ ‘)’
Push Item(scan)
else
Push Item(scan)
If (Item(scan) = ‘)‘ )
Push Item(scan)
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) )
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) )
Prefix array = pop
Discard ‘(‘ ‘)’
Push Item(scan)
else
Push Item(scan)
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) )
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) )
Prefix array = pop
Discard ‘(‘ ‘)’
Push Item(scan)
else
Push Item(scan)
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) )
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) )
Prefix array = pop
Discard ‘(‘ ‘)’
Push Item(scan)
else
Push Item(scan) )
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) )
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) )
Prefix array = pop
Discard ‘(‘ ‘)’
Push Item(scan)
else
Push Item(scan) )
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) )
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) )
Prefix array = pop
Discard ‘(‘ ‘)’
Push Item(scan)
else
Push Item(scan) 1
)
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) )
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) )
Prefix array = pop
Discard ‘(‘ ‘)’
Push Item(scan)
else
Push Item(scan) 1
)
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) )
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) )
Prefix array = pop
Discard ‘(‘ ‘)’
Push Item(scan)
else
Push Item(scan) 1
)
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) ) Item(stack) Item(scan)
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) ) ) +
Prefix array = pop
Discard ‘(‘ ‘)’
Push Item(scan)
else
Push Item(scan) 1
)
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) ) Item(stack) Item(scan)
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) ) ) +
Prefix array = pop
Discard ‘(‘ ‘)’
Push Item(scan)
else +
Push Item(scan) 1
)
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) )
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) )
Prefix array = pop
Discard ‘(‘ ‘)’
Push Item(scan)
else +
Push Item(scan) 1 1
)
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) )
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) )
Prefix array = pop
Discard ‘(‘ ‘)’
Push Item(scan)
else +
Push Item(scan) 11
)
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) )
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) )
Prefix array = pop
Discard ‘(‘ ‘)’
Push Item(scan)
else +
Push Item(scan) 1 1
)
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) ) Item(stack) Item(scan)
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) ) + (
Prefix array = pop
Discard ‘(‘ ‘)’
Push Item(scan)
else +
Push Item(scan) 1 1
)
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) ) Item(stack) Item(scan)
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) ) + (
Prefix array = pop
Discard ‘(‘ ‘)’
Push Item(scan)
else +
Push Item(scan) 1 1 +
)
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) ) Item(stack) Item(scan)
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) ) + (
Prefix array = pop
Discard ‘(‘ ‘)’
Push Item(scan)
else (
Push Item(scan) 1 1+
)
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) )
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) )
Prefix array = pop
Discard ‘(‘ ‘)’
Push Item(scan)
else (
Push Item(scan) 1 1+
)
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) ) Item(stack) Item(scan)
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) ) ( /
Prefix array = pop
Discard ‘(‘ ‘)’
Push Item(scan)
else (
Push Item(scan) 1 1+
)
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) ) Item(stack) Item(scan)
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) ) ( /
Prefix array = pop
Discard ‘(‘ ‘)’
Push Item(scan) /
else (
Push Item(scan) 1 1 +
)
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) )
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) )
Prefix array = pop
Discard ‘(‘ ‘)’
Push Item(scan) /
else (
Push Item(scan) 1 1 +4
)
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) )
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) )
Prefix array = pop
Discard ‘(‘ ‘)’
Push Item(scan) /
else (
Push Item(scan) 1 1 +4
)
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) )
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) )
Prefix array = pop
Discard ‘(‘ ‘)’
Push Item(scan) /
else (
Push Item(scan) 1 1 +4
)
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) ) Item(stack) Item(scan)
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) ) / /
Prefix array = pop
Discard ‘(‘ ‘)’
Push Item(scan) /
else (
Push Item(scan) 1 1 +4
)
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) ) Item(stack) Item(scan)
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) ) / /
Prefix array = pop
Discard ‘(‘ ‘)’ /
Push Item(scan) /
else (
Push Item(scan) 1 1 +4
)
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) )
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) )
Prefix array = pop
Discard ‘(‘ ‘)’ /
Push Item(scan) /
else (
Push Item(scan) 1 1 + 4 8
)
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) )
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) )
Prefix array = pop
Discard ‘(‘ ‘)’ /
Push Item(scan) /
else (
Push Item(scan) 1 1 +4 8
)
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) )
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) )
Prefix array = pop
Discard ‘(‘ ‘)’ /
Push Item(scan) /
else (
Push Item(scan) 1 1 +48
)
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) ) Item(stack) Item(scan)
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) ) / +
Prefix array = pop
Discard ‘(‘ ‘)’ /
Push Item(scan) /
else (
Push Item(scan) 1 1 +48
)
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) ) Item(stack) Item(scan)
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) ) / +
Prefix array = pop
Discard ‘(‘ ‘)’ /
Push Item(scan) /
else (
Push Item(scan) 1 1 + 4 8 /
)
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) )
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) )
Prefix array = pop
Discard ‘(‘ ‘)’
Push Item(scan) /
else (
Push Item(scan) 1 1 + 4 8 / /
)
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) )
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) )
Prefix array = pop
Discard ‘(‘ ‘)’
Push Item(scan)
else
Push Item(scan) 1 1 + 4 8 / /
+
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) )
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) )
Prefix array = pop
Discard ‘(‘ ‘)’
Push Item(scan)
else
Push Item(scan) 1 1 +4 8 / / 3
+
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) )
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) )
Prefix array = pop
Discard ‘(‘ ‘)’
Push Item(scan)
else
Push Item(scan) 1 1 + 4 8 / / 3
+
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) )
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) )
Prefix array = pop
Discard ‘(‘ ‘)’
Push Item(scan)
else
Push Item(scan) 1 1 + 4 8 / / 3
+
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) ) Item(stack) Item(scan)
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) ) + -
Prefix array = pop
Discard ‘(‘ ‘)’
Push Item(scan)
else
Push Item(scan) 1 1 + 4 8 / / 3 3
+
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) ) Item(stack) Item(scan)
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) ) + -
Prefix array = pop
Discard ‘(‘ ‘)’
Push Item(scan)
else -
Push Item(scan) 1 1 + 4 8 / / 3 3
+
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) )
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) )
Prefix array = pop
Discard ‘(‘ ‘)’
Push Item(scan)
else -
Push Item(scan) 1 1 +4 8 / / 3 3
+
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) )
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) )
Prefix array = pop
Discard ‘(‘ ‘)’
Push Item(scan)
else -
Push Item(scan) 1 1 + 4 8 / / 3 3
+
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) )
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) )
Prefix array = pop
Discard ‘(‘ ‘)’
Push Item(scan)
else -
Push Item(scan) 1 1 + 4 8 / / 3 3
+
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) ) Item(stack) Item(scan)
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) ) - *
Prefix array = pop
Discard ‘(‘ ‘)’
Push Item(scan)
else -
Push Item(scan) 1 1 + 4 8 / / 3 3
+
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) ) Item(stack) Item(scan)
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) ) - *
Prefix array = pop
Discard ‘(‘ ‘)’
Push Item(scan) *
else -
Push Item(scan) 1 1 + 4 8 / / 3 3
+
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) )
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) )
Prefix array = pop
Discard ‘(‘ ‘)’
Push Item(scan) *
else -
Push Item(scan) 1 1 + 4 8 / / 3 3 2
+
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Data Structure –Stack Application
Infix to Prefix 2*3-3+8/4/(1+1)
●
Reverse string and Scan the Expression from left to right
●
If (Item(scan) = Operand) )1+1(/4/8+3–3*2$
Add Item(scan) into Prefix array
●
else
If (stack = Empty)
Push Item(scan)
If ( (prec)Item(stack) >(prec) Item(scan) )
Prefix array = pop
while((prec)Item((top)) >(prec) Item(top -1) )
Prefix array = pop
Discard ‘(‘ ‘)’
Push Item(scan) *
else -
Push Item(scan) 1 1 + 4 8 / / 3 3 2 * - +
+
If (Item(scan) = ‘)‘ ) Prefix array
Push Item(scan) Stack
If (Item(scan) = ‘(‘ )
Prefix array = Pop all items till ‘)‘
Discard ‘(‘ ‘)’
●
If EOE
Empty Stack and discard '(' ')'
Evaluation -Infix to Prefix