You are on page 1of 29

UNIT II LINEAR DATA

STRUCTURES – STACKS AND


QUEUES

Evaluating Arithmetic
Expressions
What is Arithmetic Expressions
• An arithmetic expression is an expression built up using numbers,
arithmetic operators (such as +, , -, / and ) and parentheses, "(" and
")".   
• Arithmetic expressions may also make use of exponents, for
example, writing  23as an abbreviation for ((2*2)*2).
• An arithmetic expression in which the only operators
are +,*, -,/ and exponentiation, is called a simple arithmetic
expression. Here are some examples:
(3 + 4) -- the  sum of numbers is an arithmetic expression
(7 - 3) --  the difference  of two  numbers is an arithmetic expression
(2 *5) -- a product of two numbers is an arithmetic expression
(23 + 5) -- this is an abbreviation for the arithmetic expression (((2*2*)2) + 5)
(23 + 5)2 -- this is an abbreviation for the arithmetic expression ((23 + 5)*(23 + 5)
Rules for Evaluating Arithmetic Expressions

1. Evaluate the expression beginning with the expression


in the  innermost parentheses and working outwards.
2. When evaluating a parenthesis-free expression
perform  the operations in the following order:
1. evaluate all factorials from left to right
2. do all multiplications from left to right
3. do additions and subtractions from left to right

The stack organization is very effective in evaluating


arithmetic expressions.
Sample Exercises
1.  7 - 6 + 2 
2. 7 - (6 + 2)
3. (5 + 6)* (2+1)
4.  ( 1 + (5*6)+( ( 2 + 3 ) * ( 4 * 5 ) ) )
5.  4/2-[(5*3)+(abs(-7))]
Notations
• Infix, Postfix and Prefix notations are three different but
equivalent ways of writing expressions. 
1) Prefix –An expression is called the prefix expression if the operator
appears in the expression before the operands. Simply of the form
(operator operand1 operand2).
E.g. +AB
2) Infix – An expression is called the infix expression if the operator
appears in the expression in between the operands. Simply of the form
(operand1 operator operand2).
E.g. A+B
3) Postfix – An expression is called the postfix expression if the
operator appears in the expression after the operands. Simply of the
form (operand1 operand2 operator).
E.g. AB+
Why do we need 3 different expressions ?

• Infix expressions are human readable but not


efficient for machine reading
• Prefix and Postfix do not need the concept of
precedence & associativity hence it becomes
highly efficient to parse expressions in prefix
or postfix formats.
Operators Precedence
Order of Operations
1) Parentheses – {}, [], ()
2) Exponents(Right to Left) –
A^B, 2^3^4
3) Multiplication & Division(Left to Right) –
A*B/C
4) Addition & Subtraction(Left to Right) –
A+ B – C
Conversion of INFIX to POSTFIX
• It is easy for humans to evaluate an infix expression.
• Consider an infix expression x + y / z. While going from left
to right we first encounter + operator, but we do not add x + y
and rather evaluate y/z, followed by addition operation.
• This is because we know the order of precedence of operators
that follows BODMAS rule.
• But, how do we pass this precedence knowledge to the
computer through an expression?
• In contrast, prefix/postfix expressions do not have to deal
with such precedence because the operators are already
positioned according to their order of evaluation.
• Hence a single traversal from left to right is sufficient to
evaluate the expression.
• In this section, we will learn about the conversion of an
arithmetic expression written in infix notation to its
equivalent expression in postfix notation using a stack.
• During such conversion, a stack is used to keep track of
the operators encountered in the infix expression.
• A variable of string type is used to store the equivalent
postfix expression.
Algorithm :- Conversion of expression from infix to postfix notation
Step 1: Create an empty string named postExp to store the converted postfix expression.
Step 2: INPUT infix expression in a variable, say inExp
Step 3: For each character in inExp, REPEAT Step 4
Step 4: IF character is a left parenthesis THEN PUSH on the Stack
ELSE IF character is a right parenthesis
THEN POP the elements from the Stack and append to string
postExp until the corresponding left parenthesis is popped
while discarding both left and right parentheses
ELSE IF character is an operator
THEN IF its precedence is lower than that of operator at the top of Stack
THEN POP elements from the Stack till an
operator with precedence less than the current
operator is encountered and append to string
postExp before pushing this operator on the
postStack
ELSE PUSH operator on the Stack
ELSE Append the character to postExp
Step 5: Pop elements from the Stack and append to postExp until Stack is empty
Step 6: OUTPUT postExp
1. Print operands as they arrive
2. If the stack is empty or contains a left parenthesis on top , push the
incoming operator onto the stack
3. If the incoming symbol is ‘(‘ , push it into stack.
4. If the incoming symbol is ‘)’ pop the stack and print the operator until left
parenthesis is found
5. If the incoming symbol has higher precedence than the top of the stack,
push it on the stack
6. If the incoming symbol has lower precedence than the top of the stack ,
pop & print the top. Then test the incoming operator against the new top
of the stack.
7. If the incoming operator has equal precedence with the top of the stack,
use associativity rule
8. At the end of the expression, pop & print all operators of the stack

associativity L to R then pop & print the top of the stack & then push the
incoming operator
associativity R to L then push the incoming operator
Evaluation of Postfix Expression

• Stacks can be used to evaluate an expression


in postfix notation.
• The Algorithm for evaluating the Postfix
expression is as below
Step 1: INPUT postfix expression in a variable, say postExp
Step 2: For each character in postExp, REPEAT Step 3
Step 3: IF character is an operand
THEN PUSH character on the Stack
ELSE POP two elements from the Stack, apply the
operator on the popped elements and PUSH the computed value
onto the Stack
Step 4: IF Stack has a single element
THEN POP the element and OUTPUT as the net result
ELSE OUTPUT “Invaild Postfix expression”
The step-by-step process of evaluation of the postfix
expression 7 8 2 * 4 / +
The step-by-step process of evaluation of the postfix
expression 7 8 2 * 4 / +
The step-by-step process of evaluation of the postfix
expression 7 8 2 * 4 / +

You might also like