Week 5 - Stack
Week 5 - Stack
STACK
Week 5
Program Studi Teknik Informatika
Fakultas Teknik – Universitas Surabaya
Introduction
• Previously you’ve learnt the Queue data type with its First
In First Out (FIFO) processing scheme
• Another common processing scheme in computer program
is Last In First Out (LIFO)
• The data structure that provides this kind of access is
known as Stack.
2
Introduction
• In Stack, the item is added and removed from one end.
• As you predict, .Net Framework has already provided this data
structure through Stack <dataType> class.
• The class maintains its elements internally using a circular
array with a specified data type.
3
Stack Class
Creating Stack Object
Stack<int> myStack = new Stack<int>();
5
Calculating Math Expression in Postfix Notation
6
Calculating Math Expression in Postfix
Notation
• The math expression: 2 + 3 * 5 is expressed in Infix notation
(operator is in the middle of operands)
• In Postfix, this expression will become: 2 3 5 * +
• We will learn how to convert Infix to Postfix notation later
on.
• For now, let’s find a way to calculate a mathematical
expression that is written in postfix notation.
7
Algorithm Calculate Postfix
1. Prepare a stack to hold the operand for the postfix
expression and the result of the operation for the postfix
expression.
2. If you found operand then push it to the stack
3. If you found operator, pop two operands from the stack,
calculate them and push it again to the stack
4. If all items in the expression has been traversed, then pop
Because all items in the expression has been traversed,
the stack to get the result
Pop the stack to get the result
Result = 84
8
Example (Calculate Postfix)
31 19 - 7 4 2 * + +
Iteration-1 Iteration-3
• 31 is operand, then Push to Stack • - is operator, then Pop 2
• Stack = 31(tos) operand from Stack
• Calculate : 31 – 19 = 12,
Push the result to Stack
Iteration-2
• Stack = 12(tos)
• 19 is operand, then Push to Stack
• Stack = 31 19(tos)
9
Example (Calculate Postfix)
31 19 - 7 4 2 * + +
Iteration-4
• 7 is operand, then Push to Stack
• Stack = 12 7(tos) Iteration-6
• 4 is operand, then Push to
Iteration-5 Stack
• 4 is operand, then Push to Stack • Stack = 12 7 4 2(tos)
• Stack = 12 7 4(tos)
10
Example (Calculate Postfix)
31 19 - 7 4 2 * + +
Iteration-7 Iteration-8
• * is operator, then Pop 2 operand • + is operator, then Pop 2
from Stack operand from Stack
• Calculate : 4 * 2 = 8, Push the • Calculate : 7 + 8 = 15, Push
result to Stack the result to Stack
• Stack = 12 7 8 (tos) • Stack = 12 15(tos)
11
Example (Calculate Postfix)
31 19 - 7 4 2 * + +
Iteration-9
• + is operator, then Pop 2 Because all items in the
operand from Stack expression has been traversed,
• Calculate : 12 + 15 = 27, Pop the stack to get the result
Push the result to Stack Result = 27
• Stack = 27(tos)
12
Converting Infix to Postfix
Converting Infix to Postfix
• The process to convert math expression from infix to postfix
can be performed with the assistance of Stack as well
• To convert infix to postfix, you have to check some
conditions related to the item read from the infix expression
and the content of the stack
14
1st Rule: Operand? Just Write.
Example:
• infix 2 * 3 + 4 will have the postfix 2 3 * 4 +
• while the 2 + 3 * 4 will give you 2 3 4 * +
• From these expressions at least we can conclude that the
order of the operand in postfix is always the same as the
order in the infix. So we can have a rule (1st rule): if the
item traversed from the infix is operand, then write it
directly to the postfix string
15
2nd Rule: Empty Stack?
16
3rd Rule: Work it out
• What should we do if we have an operator in the stack and now
the item read is another operator?
• Consider the cases:
2+3*4
2*3+4
• To find it, write down the expected postfix and:
o The current condition of the postfix string
o The content of the stack
o The operator that is currently read from string then decide what
to do to find the expected this will become our 3rd rule)
17
3rd A Rule
18
3rd B Rule
19
4th Rule: No more item in Infix?
• As you work for the 3rd rule, you may already find the 4th
rule.
• 4th Rule: What should we do if there is no more item to be
traversed from the infix notation?
• pop all items from stack and put them in the postfix string
20
Example (Infix to Postfix)
2-3/4*5
Iteration-1 Iteration-2
• 2 is operand (1st Rule), • - is operator and Stack still empty
then write to Postfix (2nd Rule), then Push to Stack
• Postfix = 2 • Postfix = 2
• Stack = <Empty> • Stack = - (tos)
21
Example (Infix to Postfix)
2-3/4*5
Iteration-3 Iteration-4
• 3 is operand (1st Rule), then • / is operator and TOS is lower (3rd
write to Postfix A Rule), Push operator to Stack
• Postfix = 2 3 • Postfix = 2 3
• Stack = - (tos) • Stack = - / (tos)
22
Example (Infix to Postfix)
2-3/4*5
Iteration-5 Iteration-6
• 4 is operand (1st Rule), • * is operator and TOS is equal (3rd
then write to Postfix B Rule), pop all operators while
• Postfix = 2 3 4 they have higher or equal
• Stack = - / (tos) hierarchy and put operators in the
postfix. Push operator *
• Postfix = 2 3 4 /
• Stack = - * (tos)
23
Example (Infix to Postfix)
2-3/4*5
24
What if Infix contains brackets?
5th Rule: Including the brackets
26
5th B Rule
27
Example (Infix to Postfix)
2*(3–4)
Iteration-1 Iteration-2
• 2 is operand (1st Rule), then • * is operator and Stack still
write to Postfix empty (2nd Rule), then Push
• Postfix = 2 to Stack
• Stack = <empty> • Postfix = 2
• Stack = * (tos)
28
Example (Infix to Postfix)
2*(3–4)
Iteration-3 Iteration-4
• ( is opening bracket (5thA • 3 is operand (1st Rule), then
Rule), then push to Stack write to Postfix
• Postfix = 2 • Postfix = 2 3
• Stack = * ( (tos) • Stack = * ( (tos)
29
Example (Infix to Postfix)
2*(3–4)
Iteration-5 Iteration-6
• - is operator and TOS is lower • 4 is operand (1st Rule), then
(3rd A Rule), then push to Stack write to Postfix
• Postfix = 2 3 • Postfix = 2 3 4
• Stack = * ( - (tos) • Stack = * ( - (tos)
30
Example (Infix to Postfix)
2*(3–4)
31
Questions???
Exercise
33