Applications of Stacks
Department of Computer Science and Software
Engineering
Capital University of Sciences & Technology,
Islamabad Pakistan
CS2143 Data Structures
Stack
Solving Complex Expressions
!
An expression is a collection of operators and operands that represents a
specific value.
CS2143 Data Structures
Order of Arithmetic
Notations
Order of
Arithmetic
Notation
Postfix Infix Prefix
operator operator
follows precedes
operands operands
CS2143 Data Structures
Operator Precedence
CS2143 Data Structures
Infix Expression
Operator is written between two operands.
Example
a+b
a*b+c
a*(b+c)
a+b+c*d+g/f
Postfix Expression
Operators follow the operands.
Infix Expression Postfix Expression
a+b ab+
a+b*c
Postfix Expression
Operators follow the operands.
Infix Expression Postfix Expression
a+b ab+
a+b*c abc*+
(a+b)*c
Postfix Expression
Operators follow the operands.
Infix Expression Postfix Expression
a+b ab+
a+b*c abc*+
(a+b)*c ab+c*
(a-b)*(c+d)
Postfix Expression
Operators follow the operands.
Infix Expression Postfix Expression
a+b ab+
a+b*c abc*+
(a+b)*c ab+c*
(a-b)*(c+d) ab-cd+*
Postfix Expression
Convert the following infix notation into
postfix expression.
(a+b)*(c-d/e)+f
Ways of Solving Expressions
CS2143 Data Structures
Infix to Postfix Conversion
Postfix expression can easily be resolved using
stacks as compared when using other types of data
structures
Infix expressions are 1st converted to Postfix
expressions based on operator precedence
Converted postfix expressions are translated into
machine code
Machine code is executed to evaluate the
expression and output the final result
Postfix Expression
6 3 +2*=
Postfix Expression Evaluation
6 3 +2*=
Read the operands until find the
operators
Perform the operation
Continue the process
Postfix Expression Evaluation
6 3 +2*=
Stack
Postfix Expression Evaluation
6 3 +2*=
Push 6
Stack
Postfix Expression Evaluation
6 3 +2*=
Push 6
Push 3 3
Stack
Postfix Expression Evaluation
6 3 +2*=
Push 6
Push 3 3
6
Operator find
Stack
Postfix Expression Evaluation
6 3 +2*=
Push 6
Push 3
Operator find : operator = +
Pop stack twice : Op2 = 3,Op1 = 6
Stack
Postfix Expression Evaluation
6 3 +2*=
Push 6
Push 3
Operator find .operator = +
Pop stack twice. Op2 = 3,Op1 = 6
Op1+op2=9
Stack
Postfix Expression Evaluation
6 3 +2*=
Push 6
Push 3
Operator find .operator = +
Pop stack twice. Op2 = 3,Op1 = 6
Op1+op2=9 9
Push 9
Stack
Postfix Expression Evaluation
6 3 +2*=
Push 6
Push 3
Operator find .operator = +
Pop stack twice. Op2 = 3,Op1 = 6
Op1+op2=9
Push 9 2
Push 2 9
Operator find.operator = * Stack
Postfix Expression Evaluation
6 3 +2*=
Push 6
Push 3
Operator find .operator = +
Pop stack twice. Op2 = 3,Op1 = 6
Op1+op2=9
Push 9
Push 2
Operator find.operator = *
Pop stack twice. Op2 = 2,Op1 = 9 Stack
Postfix Expression Evaluation
6 3 +2*=
Push 6
Push 3
Operator find .operator = +
Pop stack twice. Op2 = 3,Op1 = 6
Op1+op2=9
Push 9
Push 2
Operator find.operator = *
Pop stack twice. Op2 = 2,Op1 = 9
Op2*op1=18 Stack
Postfix Expression Evaluation
6 3 +2*=
Push 6
Push 3
Operator find .operator = +
Pop stack twice. Op2 = 3,Op1 = 6
Op1+op2=9
Push 9
Push 2
Operator find.operator = *
Pop stack twice. Op2 = 2,Op1 = 9 18
Op2*op1=18
Push 18 Stack
Postfix Expression Evaluation
6 3 +2*=
Push 6
Push 3
Operator find .+
Pop stack twice. Op2 = 3,Op1 = 6
Op1+op2=9
Push 9
Push 2
Operator find. *
Pop stack twice. Op2 = 2,Op1 = 9 18
Op2*op1=18
Stack
Push 18
Operator find =
Postfix Expression Evaluation
6 3 +2*=
Push 6
Push 3
Operator find .+
Pop stack twice. Op2 = 3,Op1 = 6
Op1+op2=9
Push 9
Push 2
Operator find. *
Pop stack twice. Op2 = 2,Op1 = 9
18
Op2*op1=18
Push 18 Stack
Operator find =
Pop
Algorithm
Read operators and push operators.
If symbol(+,/,-,*,%) find, perform two pop
operation.
If symbol is =,expressions end.
Perform pop and print the answer.
Examples
3+2*5=
(4+5)/(6*2)=
Input File
#6 #3 + #2 * =
Every operand first has # .
Main Algorithm
We need two functions
Evaluate Expression
Evaluate Operators
Evaluate Expression
Read the character.
While character is not equal to =
If character is equal to #{
Read integer
Push integer into Stack.}
Else{ Evaluate the operators}
Read the next character
Evaluate Expression
Evaluate Operands
If Stack is empty : error.
Else pop the 2nd operands.
Again check if stack is empty. If it is, throw
error
Else pop the 1st operand.
Perform the operation according to operator,
then push the result in the stack.
Evaluate Operands
Evaluate Operands
Main Function
Illegal Operations????
Steps to Convert Infix Expression to
Postfix Expression
Read all the symbols one by one from left to right in the given Infix
Expression
If the reading symbol is operand, then directly print it to the result
(Output)
If the reading symbol is left parenthesis ‘(‘, then Push it on to the
Stack
If the reading symbol is right parenthesis ‘)’, then Pop all the
contents of the stack until respective left parenthesis is poped
Print each poped symbol to the result
If the reading symbol is operator (+,-,/,%,*, etc.), then Push it on to
the Stack.
However, first pop the operators which are already on the stack that
have higher or equal precedence than current operator and print
them to the results
Example
Consider the following Infix expression:
(A + B) * (C - D)
CS2143 Data Structures
Expression : (A + B) * (C - D)
CS2143 Data Structures
Expression : (A + B) * (C - D)
CS2143 Data Structures
Expression : (A + B) * (C - D)
CS2143 Data Structures
Expression : (A + B) * (C - D)
CS2143 Data Structures
Expression : (A + B) * (C - D)
CS2143 Data Structures
Expression : (A + B) * (C - D)
CS2143 Data Structures
Expression : (A + B) * (C - D)
CS2143 Data Structures
Expression : (A + B) * (C - D)
CS2143 Data Structures
Expression : (A + B) * (C - D)
CS2143 Data Structures
Expression : (A + B) * (C - D)
CS2143 Data Structures
Expression : (A + B) * (C - D)
CS2143 Data Structures
Expression : (A + B) * (C - D)
CS2143 Data Structures
Expression : (A + B) * (C - D)
Final Expression : AB + CD -*
CS2143 Data Structures
Balancing Symbols
Required during compile time of our written
programs
Stacks help check for syntax errors by
identifying missing braces in our code
Assignment
Write a COMPLETE program which can
Take input in infix expression
convert infix expression to postfix expression
evaluate postfix expression and display the result
Bonus Task
Write a code to balance parenthesis in a given infix
expression using dynamic stacks
• ASSIGNMENT SUBMISSION DEADLINE Oct 14, 2019
• PLAGIARISM IS UNACCEPTABLE!
• THIS TASK WILL BE MARKED BASED ON SUBMITTED
CODE AND DEMO!
CS2143 Data Structures