Professional Documents
Culture Documents
Reversing Data
Parentheses Matching
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
a ( b ( c ) ) ) f
1 2 1 2 3
A + B
operand operator operand
Infix Notation (cont)
Example:
A + ( B * C )
(a+b)*c
Example:
+ab
+a*bc
*+abc
Postfix Notation
Alternative to infix as well.
Example:
ab+
abc*+
ab+c*
Example All Notations
( ( 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 ) )
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.
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
1) A B * C – D +
2) A B C + * D -