Stacks can be used for two main applications: (1) evaluating postfix expressions by pushing operands and popping to apply operators, and (2) converting infix expressions to postfix by pushing operands and operators and popping to output in proper order based on operator precedence. The document provides algorithms and examples for each application. It explains how for postfix evaluation an array represents the expression which is iterated, pushing operands and applying operators by popping. For infix to postfix, it pushes operands and operators to a stack while popping to output the postfix expression based on operator precedence and parentheses.
Stacks can be used for two main applications: (1) evaluating postfix expressions by pushing operands and popping to apply operators, and (2) converting infix expressions to postfix by pushing operands and operators and popping to output in proper order based on operator precedence. The document provides algorithms and examples for each application. It explains how for postfix evaluation an array represents the expression which is iterated, pushing operands and applying operators by popping. For infix to postfix, it pushes operands and operators to a stack while popping to output the postfix expression based on operator precedence and parentheses.
Stacks can be used for two main applications: (1) evaluating postfix expressions by pushing operands and popping to apply operators, and (2) converting infix expressions to postfix by pushing operands and operators and popping to output in proper order based on operator precedence. The document provides algorithms and examples for each application. It explains how for postfix evaluation an array represents the expression which is iterated, pushing operands and applying operators by popping. For infix to postfix, it pushes operands and operators to a stack while popping to output the postfix expression based on operator precedence and parentheses.
2. Transforming Infix Expressions into Postfix Expressions 1. Evaluation of Postfix Expression Algorithm: Input: Given an arithmetic expression in Postfix notation. Output: Value of the arithmetic expression 1. Array = (arithmetic expression) 2. For I -> 1 to Array.length 3. If (Array[ I ] = operand ) 4. Push (Array[I]) 5. Else if (Array[I] = operator) 6. A = Pop() 7. B = Pop() 8. Result= B (operator) A 9. Push(Result) 10. Value = Top() 11. Exit
2. Transforming Infix Expressions into Postfix Expressions
Algorithm: Input: Given an arithmetic expression in Infix notation. Output: Postfix notation of the given arithmetic expression is returned 1. Push(Stack, ( ) 2. Add ) at the end of arithmetic expression 3. Array = (arithmetic expression) 4. For I -> 1 to Array.length 5. If (Array[I]=operand) 6. Add (Postfix_Exp, Array[I]) 7. Else if (Array[I] = () 8. Push(Stack, Array[I]) 9. Else if (Array[I] = operator ) 10. A=Pop(Stack) 11. While(A is operator & precedence(A) > = precedence (Array[I]) ) 12. Add(Postfix_Exp, A) 13. A=Pop(Stack) 14. Push(Stack, A) 15. Push(Stack, Array[I]) 16. Else if (Array[I] = )) 17. A=Pop(Stack) 18. While(A != ( ) 19. Add(Postfix_Exp, A) 20. A=Pop(Stack) 21. Remove(A) // remove and do not add ( to Postfix_Exp 22. Exit