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.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
68 views3 pages
Applications of Stacks HGJ
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.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3
Applications of Stacks
1. Evaluation of Postfix Expression
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