Stack - Converting Infix Notation to Postfix Notation and Vice Versa


This code uses its own stack of Strings to proceed with the convertion from Infix/Postfix to Postfix/Infix.








Converting Infix to Postfix Notation Algorithm

1.     If recognize an operand, display

2.     If recognize a ‘(‘, push it on the stack

3.     If recognize a ‘)’

a.     Pop and display until encountering the first ‘(’ inside the stack

b.     Pop the ‘(’ from the stack

4.     If recognize an operator

a.     Peek from the stack and compare to the operator

b.     If stack is empty, push it on the stack

c.     If stack is not empty

                                          i.    If top of the stack is ‘(‘, push it on the stack

                                         ii.    If top of the stack is operator

1.     If top of the stack is of higher precedence, pop and display until encountering the first ‘(’ or if stack is empty

2.     If top of the stack is of lower precedence, push it on the stack

3.     If top of the stack is of equal precedence, pop and display then push it on the stack.

5.     If done reading, pop and display till stack is empty.

Converting Postfix to Infix Notation Algorithm

1.     If recognize an operand, push it on the stack

2.     If recognize an operator, pop its operands (pop 2 operands), and apply the operator and push the value on the stack.

o    If an item popped is already an expression, enclose the expression with ‘(‘ and ‘)’

3.     Upon conclusion, the value of the postfix expression is on the top of the stack.

 

-       The algorithm is based on the following assumptions:

1.     Each operand is denoted as a single alphabetic character.

2.     The expression may only 


Click here for the code

2 comments:

Unknown said...

link for the code is not valid. it says "The file link that you requested is not valid. Please contact link publisher or try to make a search." please fix this

Unknown said...

where is the code ?

Post a Comment