You are on page 1of 35

Objective:

 To introduce stack application.


 To understand infix, postfix and prefix operations.
Stack Applications
Some examples on stack applications:

 Reversing Data

 Converting Binary to Decimal

 Parentheses Matching

 Infix to Prefix and Postfix Transformation

 Evaluating Postfix Expressions

2
Reversing Data
Given a set of data to be reordered so that the first
and last elements are exchanged with all the
positions between first and last being exchanged
also.

Example:
Set of data { 5 6 7 8} becomes {8 7 6 5}
Converting Decimal to Binary
Converts Decimal: 4 to Binary
i)number = 4
ii)digit = 4 % 2 = 0
iii) Push(digit) 0 Stack
iv)number = number/2= 2
v)digit = 2 % 2 = 0

vi) Push(digit) 0
vii) number = number/2= 1 0 Stack
viii) digit = 1 % 2 = 1

ix) Push(digit) 1
x)number = number/2 = 0 0
xi)Pop() 0 Stack
xii) Pop()
xiii) Pop()

0
0 1 0 10 100
Algorithm Converting Decimal to Binary
1.create Stack
2.prompt (Enter a decimal to convert to binary)
3.read (number)
4. loop (number > 0)
 digit = number % 2
 if Stack no full
 push(digit)
 else
 - print (Stack overflow)
 - quit algorithm
 end if
 number = number / 2
5. end loop
6. loop (not emptyStack(stack))
 popStack()
 print (digit)
7. end loop  
Parentheses Matching
Stack can be used to check the parentheses matching
in an equation.
Using LIFO – Last In First Out.
Example:
 abc ( def ) ( ghi ( klm ) p : not match.
 abc ( def ) ghi : match
Parentheses Matching (cont)
 How do we implement this using a stack? 
LIFO

1. Scan expression from left to right


2. When a left parenthesis “(“ is encountered, add its
position (or parentheses) to the stack using push
operation.
3. When a right parenthesis “)“ is encountered,
remove matching position(or matching
parentheses) from the stack using pop operation.
Parentheses Matching (cont)

a ( b ( c ) ) ) f
1 2 1 2 3

For each ‘ ( ‘ , push into stack.


indeks
For each ‘ ) ‘, pop ‘ ( ‘ from stack.
2
( ) - for the first matched.
1 ( ( ) - for the second matched.
0 ( For the third ‘)’ : no more ‘(‘  NOT
MATCH
Parentheses Matching (cont)
 Parenthesis is not match in an equation if :
At the end of equation, every ‘ ( ’ and ‘ ) ’ is already been
matched. But,

1.If there is still one left ‘ ( ’ in stack. OR


2.If there is still ‘ ) ’ but stack is already empty.

This means it is not a match.


Algorithm Parentheses Matching
create ( s );
continue = true;
while ( not end of input ) && continue {
ch = getch( );
if ch = ‘ ( ‘ || ch = ‘ )’ {
if ch = ‘(‘
push(s, ‘ ( ‘ );
else IsEmpty(s)
continue = false;
else
Pop(s)
}
}
if ( end of input ) && IsEmpty(s)
cout <<”Balanced”;
else
cout <<“ Not Balanced”;
Infix to Prefix and Postfix
Transformation
Expression can be checked using :
1. Infix notation.
2. Prefix notation.
3. Postfix notation.
Infix Notation

Usually operator will be at the middle of operand.

A + B
operand operator operand
Infix Notation (cont)
Example:
A + ( B * C )
(a+b)*c

To evaluate this expression, these rules must be


used :
Precedence rules : (operator ranking)
Association rules : (left to right)
Parenthesis ranking
Prefix Notation
Alternative to infix.

Operator will be before the operand in the


expression.

Example:
+ab
+a*bc
*+abc
Postfix Notation
Alternative to infix as well.

Operator will be after the operand.

Example:
ab+
abc*+
ab+c*
Example All Notations

Infix Prefix Postfix


a+b +ab ab+
a+(b*c) +a*bc abc*+
(a+b)*c *+abc ab+c*
Advantages of Prefix and Postfix
No need to use:
Precedence rules (operator ranking)
Association rules (left to right)
Parantheses rules
Steps to Change Infix to Prefix
Example : b + c * 3 / 2 - 4
Step 1 :
Complete the parentesis in infix notation --> using precedence,
associations and parenthesis rules as a guideline.

( ( b + ( ( c * 3 ) / 2 ) ) - 4 )
1
2

4
Steps to Change Infix to Prefix (cont)
Step 2 :
Move the operator of the nearest left parenthesis.

- + b / * c 3 2 4
1

4
Steps to Change Infix to Prefix (cont)
Other example:
1. a + b
=(a+b)
= +ab

2. a + ( b * c )
=(a+(b*c))
=+a*bc
Steps to Change Infix to Postfix
Example: a + b / c
Step 1 :
Complete the parentesis in infix notation --> using precedence,
associations and parenthesis rules as a guideline.

( a + ( b / c ) )
1

2
Steps to Change Infix to Postfix (cont)
Step 2 :
Move the operator of the nearest right parenthesis.

a b c / +
1

2
Steps to Change Infix to Postfix (cont)
Other example
1. a+b
= (a+b)
=ab+

2. a + b * c
=(a+(b*c))
= abc*+

3. a + b * ( c – d ) / ( p – r )
=a+(b*(c–d))/(p–r)
=(a+((b*(c–d))/(p–r)))
=abcd-*pr-/+
Algorithm to Change Infix to Postfix
1. Create one empty stack.
2. While not met the ending statement
a.  Read token
b.  If token is:
        i.  (  push into stack
ii. )  pop and display the element of stack until find bracket ‘(‘.
Note: error occur if ‘(‘ not found
iii. Operator
1. If stack is empty or have token with higher precedence from
the stack top
push token into stack.
2.Else pop and display top, then repeat
compare new token with the new top.
iv. Operand
1. Display.
3. When the end statement is found, pop and display stack item until stack
is empty.  
Algorithm to Change Infix to Postfix (cont)
create ( s ); if ( ch is operator )
push ( s, ‘#’ ); { while ( !isEmpty(s) &&
while ( not end of infix input ) ( precedence ( stacktop( ) ) >=
{ ch = getch // next input precedence ( ch )) )
character { ch = pop ( s );
add ch into postfix;
if ( ch is operant )
add ch pd notasi postfix; }
push ( s, ch );
if ( ch == ‘ ( ‘ )
push ( s, ch ) }// if
if ( ch == ‘ ) ‘ ) } // while
{ pop ( s ); while ( stacktop( ) != ‘#’ )
while ( ch !== ‘ ( ‘ ) { ch = pop ( s );
{ add ch pd notasi postfix; add ch pd notasi postfix;
ch = pop ( s ); }
}
}
Alternative Steps to Change Infix to Postfix

Example 1:
Input Stack Output
A+B*C Empty Empty
A+B*C
+B*C Empty A
 

B*C + A
*C + AB
C +* AB
+* ABC
+ ABC*
Empty ABC*+
Alternative Steps to Change Infix to Postfix

ExampleInfix
2: Stack Postfix
A + B * C – D / E #
+ B * C – D / E # A
A + B * C – BD* /CE–   D / E # + A
* C – D / E # + A B
C – D / E # + * A B C
– D / E # + * A B C * +
D / E # - A B C * + D
/ E # - A B C * + D
E # - / A B C * + D E
# - / A B C * + D E
# A B C * + D E / -
Alternative Steps to Change Infix to Postfix

Example Infix
3: Stack Postfix
A * B – (C +D)+E #

* B – (C +D)+E # A

A * B – ( C +B D )+E
– (C +D)+E
 
# * A
– (C +D)+E # * A B
(C +D)+E # A B *
C +D)+E # - A B *
+D)+E # - ( A B *
D)+E # - ( A B * C
)+E # - ( + A B * C
+E # - ( + A B * C D
E # - A B * C D +
# A B * C D + -

# + A B * C D + -

# + A B * C D + - E

# A B * C D + - E +
Evaluating Postfix Expression
One of the function of compiler is to evaluate the
mathematical expression .

Example
y = x + z * ( w / x + z * ( 7 + 6 ) )

Compiler must capable to check the expression is


valid or not (syntax).
Evaluating Postfix Expression (cont)
Step 1 :
If the character is an operant, it will be pushed into the
stack  using push().

Step 2 :
If the character is an operator, two latest operant will be
pop out from the stack  using pop().
Example : pop (opr1) and pop(opr2)

Step 3:
The value of arithmetic operation from operant1 and
operant 2 ( opr1 operator opr2 ) will be pushed into the
stack  using push().
Evaluating Postfix Expression (cont)
Step 4:
Step 1 till step 3 will be loop until no more
characters in notation.

At the end of the operation, there will be ONLY


one value is left in the stack  the value of
arithmetic notation.
Evaluating Postfix Expression (cont)

246+*
Postfix
46+* 2
4
2
6+* 6
Stack
4
2
a) +* 10
2
b) 4+6=10
*

20 2 * 10 = 20
c)
Algorithm to Value the Postfix Expression
create ( s )
while ( not end of the postfix notation ){
ch = get char;
if ( ch adalah operan )
push ( s, ch ); // insert dlm stack
else { // jika ch = operator
operan1 = pop( s );
operan2 = pop( s );
hasil = operan1 ch operan2;
push( s, hasil );
}
}
Exercises
 Transformation infix to postfix:
Exercises:

1) (A + B) * C
2) A + B * C – D / E
3) (A + B) * C – D * F + C
4) A * B + C * D

 Evaluating postfix expressions:


Exercises:

1) A B * C – D +
2) A B C + * D -

where A = 2, B = 3, C = 4 and D =5.


You might also like