0% found this document useful (0 votes)
220 views23 pages

Stack Applications: CS501 Irena Pevac

The document discusses arithmetic and boolean expressions in infix, prefix, and postfix notation and provides algorithms for evaluating expressions in postfix notation and converting infix expressions to postfix notation using a stack. It also describes algorithms for checking matching parentheses in an expression.

Uploaded by

hhqhamza
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
220 views23 pages

Stack Applications: CS501 Irena Pevac

The document discusses arithmetic and boolean expressions in infix, prefix, and postfix notation and provides algorithms for evaluating expressions in postfix notation and converting infix expressions to postfix notation using a stack. It also describes algorithms for checking matching parentheses in an expression.

Uploaded by

hhqhamza
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

Stack Applications

CS501
Irena Pevac
Arithmetic Expressions
• Arithmetic expressions have
– operands (variables or numeric constants).
– Operators
• Binary : +, -, *, / ,%
• Unary: -
Priority convention:
- Unary minus (sign for negative numbers) has highest
priority
*, /, % have medium priority
+, - have lowest priority
Infix, Prefix, Postfix
• Example: arithmetic expression a & b consists of
operands a, b and operator &.
– Infix notation is format where operator is specified in
between the two operands. a&b
– Prefix notation is format where operator is specified
before the two operands. &ab
– Postfix notation is format where operator is specified
after the two operands. Postfix notation is also called
RPN or Reverse Polish Notation. a b &
Arithmetic Expressions
Prefix Notation Infix Notation Postfix Notation

+A * B C A+B*C ABC*+

*+ABC (A+B) * C AB+C*

+–ABC A–B+C AB–C+

–A+BC A – (B+C) ABC+–


Boolean Expressions
• Boolean expressions have
– Operands (variables of boolean type or boolean
constants TRUE, FALSE).
– Operators Binary : &, V, =>, 
Unary: ~
• Convention for operator priorities:
– ~ has highest priority, & has next priority
level, v has next priority level, => has next
priority level,  has lowest priority.
Infix, Prefix, Postfix for Boolean
Expressions
• Example: Boolean expression a &b consists of operands
a, b and operator &.
– Infix notation is format where operator is specified in
between the two operands. a&b
– Prefix notation is format where operator is specified
before the two operands. &ab
– Postfix notation is format where operator is specified
after the two operands. Postfix notation is also called
RPN or Reverse Polish Notation. a b &
Boolean Expressions
Prefix Notation Infix Notation Postfix Notation

vA&B C AvB&C ABC&v

&vABC (AvB) & C ABvC&

v &A B C A&BvC AB&Cv

&AvBC A & (BvC) ABCv&


Evaluating Arithmetic
Expressions in Postfix Notation
• Algorithm:
• INPUT: list of tokens that are either
numbers or binary arithmetic operators +,
-,*, /, and %. List represents postfix
notation of an arithmetic expression
• OUTPUT: value of expression.
• Create an empty stack that will contain operands.
• Take one by one token from the left to right.
– If token is an operand push it to the stack.
– If token is an operator op
• Pop the top item from the stack as operand2.
• Pop again the top item from the stack as operand1.
• Perform operation operand1 op operand2.
• Push result back to stack.
• When all tokens in input expression are processed
stack should contain a single item, which is the
value of expression.
Evaluate 3 2 - 4 *
Current Token Stack Content What was done
After Processing
the Token (Top is
at the right side)
Push 3
3 3
Push 2
2 32
Pop 2 as second operand,
- 1 pop 3 as first operand, 3-2 is
1. Push 1.
Push 4
4 14
Pop 4 as second operand,
* 4 pop 1 as first operand, 1*4 is
4. Push 4.
Evaluating Boolean Expressions
in Postfix Notation
• Algorithm is same as for arithmetic
expressions:
• INPUT: list of tokens that are either TRUE
or FALSE or operators &, V, =>, . List
represents postfix notation of an arithmetic
expression.
• OUTPUT: value of expression that is either
TRUE or FALSE.
• Create an empty stack that will contain
boolean operands.
• Take one by one token from the left to right.
– If token is an operand push it to the stack.
– If token is an operator op
• Pop the top item from the stack as operand2.
• Pop again the top item from the stack as operand1.
• Perform operation operand1 op operand2.
• Push result back to stack.
• When all tokens in input expression are
processed stack should contain a single item,
which is the boolean value of expression.
Evaluate True True & False =>
Current Token Stack Content What was done
After Processing
the Token (Top is
at the right side)
Push True
True True
Push True
True True True
Pop True as second operand,
& True pop True as first operand,
True&True is true. Push
True.
Push False
False True False
Pop False as second operand,
=> False pop True as first operand,
True =>False is False. Push
False onto stack.
ALGORITHM TO CONVERT AN
INFIX EXPRESSION TO RPN
Accepts: An infix expression
Convert the expression to RPN
Uses a stack to store operations
Output: The RPN expression
INFIX EXPRESSION TO RPN
Initialize an empty stack of operators.
While no error has occurred and the end of the infix expression
has not been reached, do the following:
a. Get the next input Token (constant, variable,
arithmetic operator, left and right parenthesis)
in the infix expression.
INFIX EXPRESSION TO RPN
b. If Token is
(i) a left parenthesis: Push it onto the stack
(ii) a right parenthesis: Pop and display stack elements until a left
parenthesis is popped, but do not display it.
(It is an error if the stack becomes empty with no
left parenthesis found.)
(iii) an operator: If the stack is empty or
Token has a higher priority
than the top stack, push Token onto the stack.
Otherwise, pop and display the top stack
element:
then repeat the comparison of Token
with the new top stack item.
A left parenthesis in the stack is assumed to
have a lower priority than that of operators.
(iv) an operand: Display it.
INFIX EXPRESSION TO RPN
c. When the end of the infix expression is
reached, pop and display stack items until
the stack is empty.
Example

Convert infix expression into postfix

(7+1)*(6+(3-2)) >> RPN


TOKEN Stack After (Top is on the Display
right)

( (
7 ( 7
+ (+ 7
1 (+ 71
) 71+
* * 71+
( *( 71+
6 *( 71+6
+ *(+ 71+6
( *(+( 71+6
(7+1)*(6+(3-2)) >> RPN
3 *(+( 71+63
- *(+(- 71+63
2 *(+(- 71+63
) *(+ 71+63-
) * 71+63-+
(7+1)*(6+(3-2)) >> RPN

• 7 1 + 6 3 - + displayed
• Stack content *
• Add that to display
• 7 1 + 6 3 - + * is RPN
Checking Parenteses
• Create an empty stack
• Until a special symbol appears (indicating the end of
expression) do
Read one by one token from the input expression
If token is
{ If the stack is empty push token to the stack
If the stack is non empty pop a top stack element
If the popped element is ( or [ type an error message such as
“Rule 2 is not satisfied at position…”
Otherwise push it back and push the token to the stack.
[ …
( …
Checking Parenteses
} If the stack is empty rule 1 is not satisfied. Type a message …
If the stack is not empty pop the top stack element and
If it is { then read next token
Otherwise type “Rule 1 is not satisfied at position …
] …
) …
Other Skip to the next token
• If the stack is non empty after the end of expression has
been encountered then print an error message such as
“Rule 1 is violated at the end of expression”
else ”Check control is O.K.”

You might also like