Algorithm:
- Say Q is an infix expression and its postfix
expression will be P. Now scan the infix expression from left to right.
- Initialize an empty stack. This is NULL.
- Repeat step 4 and 5 until all characters of Q are
scanned.
- If the scanned character is an operator, add it
to P.
- If the scanned character is an operand thenà
- If stack is empty then add it
to P
- Otherwise repeatedly pop from
the stack and add to P. Each operator which has the same or higher
precedence.
- Push operator to the stack.
- If scanned character is “( then push it to the
stack.
- If scanned character is “)” thenà
- Repeatedly pop from stack and
add to P with each operator until “(“ is encountered.
- Remove “(“ from the stack.
- If scanned finish but stack will not be empty,
then add stack to P.
Finally we’ll get the postfix
expression in “P” .
Example:
Q
= 5 * (6 + 2) – 12/4
|
Stack
|
P
|
5
*
(
6
+
2
)
-
12
/
4
Finish
|
*
*(
*(
*(+
*(+
*
-
-
-/
-/
|
5
5
5
56
56
562
562+
562+*
562+*12
562+*12
562+*124
562+*124/-
|
If you further face any problem to this you must comment below. Thanks.
For more, Go to--> http://tech-world6.blogspot.com/2016/04/infix-to-postfix-conversion-algorithm.html
For more, Go to--> http://tech-world6.blogspot.com/2016/04/infix-to-postfix-conversion-algorithm.html
No comments:
Post a Comment