0% found this document useful (0 votes)
6 views

stack-applications

The document discusses various applications of stacks, including reversing lists, checking parentheses, and converting and evaluating expressions in different notations (infix, prefix, postfix). It explains the process of converting infix expressions to postfix and prefix notations, as well as the evaluation methods for postfix and prefix expressions. Additionally, it covers operator precedence, associativity, and parentheses matching techniques.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

stack-applications

The document discusses various applications of stacks, including reversing lists, checking parentheses, and converting and evaluating expressions in different notations (infix, prefix, postfix). It explains the process of converting infix expressions to postfix and prefix notations, as well as the evaluation methods for postfix and prefix expressions. Additionally, it covers operator precedence, associativity, and parentheses matching techniques.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Applications of Stack

Dr. Prabhu Prasad B M


CSE, IIITDWD
Applications of Stack

● Reversing a list/string
● Parentheses checker
● Conversion of an infix expression into a prefix/postfix
expression
● Evaluation of a prefix/postfix expression
● Recursion int fact(x) {
if (x == 1) {
return 1;
● Tower of Hanoi } else {
return x * fact(x-1);
}
}


Applications of Stack

● Reversing a list/string
● Parentheses checker
● Conversion of an infix expression into a prefix/postfix
expression
● Evaluation of a prefix/postfix expression
● Recursion
● Tower of Hanoi

● More applications:
− Page-visited history in a Web browser
− Undo sequence in a text editor
Infix Prefix and Postfix Notations
● Infix, postfix, and prefix notations are three different but equivalent
notations of writing algebraic expressions.
● Infix: operators placed between operands:
● A+B
● Prefix: operands placed after operators (Polish Notation):
● +AB
● Postfix (Reverse Polish Notation): operands appear before their
operators:-
● AB+


Infix Prefix and Postfix Notations
● Computers find it difficult to parse infix expressions
● Information is needed about operator precedence and associativity
rules, and brackets
● Use prefix and postfix notations.
Infix to Postfix Conversion
Infix to Postfix Conversion
Infix to Postfix Conversion
● Postfix Notation
− The operands maintain the same order as in the equivalent infix
expression.
− The parentheses are not needed to designate the expression
un-ambiguously.
− While evaluating the postfix expression the priority of the
operators is no longer relevant.
Infix to Postfix Conversion
● Postfix Notation
− Convert the following Infix expressions to their postfix equivalent
● (A + B) / (C + D) – (D * E)
● a + b * c + (d * e + f) * g
Infix to Postfix Conversion
● Postfix Notation
− Convert the following Infix expressions to their postfix equivalent
● (A + B) / (C + D) – (D * E)
− AB+CD+/DE*–
● a + b * c + (d * e + f) * g
− abc*+de*f+g*+
Operator precedence and associativity

● Precedence: Priority to an operator in the given expression

● common to have multiple operators in C language


● the compiler first evaluates the operater with higher precedence.

● Associativity:
● If operators in an expression have the same precedence, how are they to be
evaluated
Operator precedence and associativity
Infix to Postfix Conversion
i. Print operands as they arrive.

ii. If the stack is empty or contains a left parenthesis on top, push the incoming operator
onto the stack.

iii. If the incoming symbol is a left parenthesis, push it on the stack.

iv. 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.

v. If the incoming symbol has higher precedence than the top of the stack, push it on
the stack.

vi. If the incoming symbol has equal precedence with the top of the stack, use
association.

i. If the association is left to right, pop and print the top of the stack and then push
the incoming operator.
ii. If the association is right to left, push the incoming operator.
vii. 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.

viii. At the end of the expression, pop and print all operators on the stack. (No
parentheses should remain.)
Infix to Postfix Conversion
Infix to Postfix Conversion
● Postfix Notation
− Convert the following Infix expressions to their postfix equivalent
● (A+B*(C-D))/E

● A + B – C * D + (E ^ F) * G / H/ I * J + K

Infix to Postfix Conversion
● Postfix Notation



Infix to Postfix Conversion
● Postfix Notation
− Convert the following Infix expressions to their postfix equivalent
● (A+B*(C-D))/E
− ABCD-*+E/
● A + B – C * D + (E ^ F) * G / H/ I * J + K
− AB+CD*-EF^G*H/I/J*+K+
Infix to Prefix Conversion
● Prefix Notation

Infix to Prefix Conversion
● Reverse the infix string. Note that while reversing the string you
must interchange the left and right parentheses.
● Obtain the postfix of the infix expression obtained from Step 1.
● Reverse the postfix expression to get the prefix expression

Infix to Postfix Conversion
Evaluating the Postfix expression
i. Push the operand onto the stack.
ii. If an operator “op1” is encountered, then follow these steps
i. Pop the two topmost elements from the stack, where Y is the
topmost element and X is the next top element below Y.
ii. Evaluate X op1 Y.
iii. Push the result onto the stack.
iii. Set the result equal to the topmost element of the stack.
Evaluating the Postfix expression
Evaluating the Prefix expression
i. Scan the prefix expression from the right one character at a time.
ii. If the scanned character is an operand, push it on the operand stack
iii. If the scanned character is an operator
i. Pop the two values from operand stack
ii. Apply the operator on the operands.
iii. Push the result onto the operand stack.
Evaluating the Prefix expression
Paranthesis Matching
i. (a+b)/(c*d)+e-f
ii. (a+b/(c*d))+e-f
iii. (a+b/(c*d)+e-f
iv. (a+b/(c*d)+e-f)
Paranthesis Matching
i. Initialize an empty stack.
ii. Traverse the input expression from left to right and for each
character, do the following:
i. If the character is an opening parenthesis ‘(‘ or ‘{‘ or ‘[‘), push
it to the stack.
ii. If the character is a closing parenthesis ‘)’ or ‘}’ or ‘]’), pop
from the stack.
i. If the popped element is the corresponding opening
parenthesis, then continue traversal. Else, the
parentheses are not balanced.
iii. After complete traversal, if there is any element left in the stack,
then the parentheses are not balanced.

You might also like