Download Data Structures & Algorithms: Stack Implementation & Expression Evaluation (Lec 19-21) and more Exercises Data Analysis & Statistical Methods in PDF only on Docsity!
Stack Implementation and Operations
Engr. Usman Raza
Application of Stacks
- It should come as no surprise that if we restrict the operations allowed on a list, those operations can be performed very quickly, i.e., low time complexity is achieved very efficiently.
- Balancing of symbols
- Compiler uses stacks to track opening and closing parenthesis in Programming
Languages
- Infix-to-postfix conversion (will be discussed in this course)
- Evaluation of postfix expression (will be discussed in this course)
- Implementing function calls (including recursion)
- Last function called (pushed) is popped first.
- Page-visited history in a Web browser [Back Buttons]
- Undo sequence in a text editor
- Matching Tags in HTML and XML
• Reversal operations and many more…….
Infix Notation
- An infix expression is a single letter, or an operator, proceeded by one infix string and followed by another Infix string.
- General form of Infix:
-
- Operand can be an integer, variable or an expression
- For Example:
- 2+3 // integers operands
- A-B // variable operands
- P*2 // one operand is integer and other is variable
- (2+3) *4 // one operand is expression other is integer
- (p+q) * (r+s) // Both operands are expressions
- Notice the format of all the above expressions.
- Two things to consider while evaluating any given infix expressions:
- Order of precedence (PEMDAS)
- Associativity Rule (Within same precedence)
Infix Notation
- 2 * 6 / 2 - 3+ 7 Order of Precedence of Operators
- Parenthesis {}, [], ()
- Exponentiation (right to left, right associative)
- Multiplication/Division (left to right, left associative)
- Addition/Subtraction (left to right, right associative)
- While evaluating an infix expression we first need to look at precedence then to resolve conflict among operator with equal precedence, we need to see associativity.
- So many things need to be done just to parse and evaluate an infix expression.
- Infix notation is the most common way of writing expressions, due to its readability: { ( 2 * 6) / 2} – (3+ 7) = 4
- But its not easy to evaluate and parse the expressions programmatically.
Postfix Notation
- A postfix expression (also called Reverse Polish Notation) is a single letter or an
operator, preceded by two postfix strings.
- Every postfix string longer than a single variable contains first and second operands
followed by an operator.
General form of Prefix:
-
- Operand can be an integer, variable or an expression
- For Example:
- Both pre and post fix can be used to evaluate the mathematical expressions.
- Programmatically, post-fix expressions are easiest to parse and least costly in terms
of memory and time.
- Post-fix is preferred mostly because of its straightforward and simple algorithm.
Example
Algorithm to Convert Infix to Postfix Q is the Expression in infix P is the output in postfix Initialization:
- PUSH “(“ on to the STACK and add “)” at end of Q.
- Scan Q from Left to Right and do the following for each element of Q until STACK is empty.
- If an operand is encountered add it to P
- If a Left (opening) parenthesis is encountered push it on to STACK
- If the stack is empty or contains a left parenthesis on top, push the incoming operator onto the stack.
- If the incoming symbol is a left parenthesis, push it on the stack.
- If the incoming symbol is a right parenthesis, pop the stack and print the operators until you see a left parenthesis. Discard the pair of parentheses.
- If the incoming symbol has higher precedence than the top of the stack, push it on the stack.
- If the incoming symbol has equal precedence with the top of the stack, use association. If the association is left to right, pop and print the top of the stack and then push the incoming operator. If the association is right to left, push the incoming operator.
- If the incoming symbol has lower precedence than the symbol on the top of the stack, pop the stack and print the top operator. Then test the incoming operator against the new top of stack.
- At the end of the expression, pop and print all operators on the stack. (No parentheses should remain.) Q: A + ( B * C - ( D / E ^ F ) * G ) * H
Stack Postfix Evaluation
Engr. Usman Raza
Algorithm to Evaluate a Postfix Expression stk = the empty stack /* scan the input string reading one element / / at a time into symb / while (not end of input) { symb = next input character; if (symb is an operand) push(stk, symb) else { / symb is an operator / opnd1 = pop(stk); opnd2 = pop(stk); value = opnd2 OPERATOR opnd1; push(stk, value); } / end else / } / end while */ return (pop(stk)); Example: Postfix Expression: 6 2 3 + - 3 8 2 / + * 2 ^ 3 + symb opnd1 (^) opnd2 value stk 6 6 2 6, 3 6,2,
- 6 5 1 1 3 6 5 1 1, 8 6 5 1 1,3, 2 6 5 1 1,3,8, / 8 2 4 1,3,
- 1 7 7 7 2 1 7 7 7, $ 7 2 49 49 3 7 2 49 49,