0% found this document useful (0 votes)
32 views

Week 5 - Stack

The document discusses the stack data structure. Some key points: - Stacks follow the LIFO (Last In First Out) processing scheme, where items are added and removed from one end. - Common stack methods include Push to add an item, Pop to remove and return the top item, and Peek to return the top item without removing it. - Stacks are useful for applications like converting infix to postfix notation and calculating mathematical expressions in postfix. An algorithm uses a stack to calculate a postfix expression by pushing operands and popping to perform operations. - Converting infix to postfix involves rules like writing operands directly, pushing operators to an empty stack, and popping operators of equal or higher

Uploaded by

tikowanaasu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Week 5 - Stack

The document discusses the stack data structure. Some key points: - Stacks follow the LIFO (Last In First Out) processing scheme, where items are added and removed from one end. - Common stack methods include Push to add an item, Pop to remove and return the top item, and Peek to return the top item without removing it. - Stacks are useful for applications like converting infix to postfix notation and calculating mathematical expressions in postfix. An algorithm uses a stack to calculate a postfix expression by pushing operands and popping to perform operations. - Converting infix to postfix involves rules like writing operands directly, pushing operators to an empty stack, and popping operators of equal or higher

Uploaded by

tikowanaasu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Data Structure

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>();

Some Common Methods:


• myStack.Push(item); //add the item to the stack
• item = myStack.Pop(); // return and remove the item at the
top of the stack
• item = myStack.Peek(); //return the item at the top of the
stack without removing it
Property
• number = myStack.Count; //gets the number of the items
contained in the stack
4
Stack Application
• Unlike Queue, it is a bit uncommon to find the stack
implementation in our everyday life
• However, stack data structure is very common to be used in
variety of computer applications, for example to:
–Convert Infix to Postfix Notation
–Calculate Postfix Notation
–Etc.

5
Calculating Math Expression in Postfix Notation

• Stack can be helpful in other application such as in the


process of calculating mathematical expression.
• As you know, 2 + 3 * 5 is equal to 17 (not 25) due to the
different hierarchy of the operator
• This calculation can be performed easily if the math
expression is expressed 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?

• What if the item traversed is operator or bracket?


• It seems that at the beginning of the traversal we can not
decide what should we do with the operator read until we
find another operator.
• Therefore we can have the 2nd rule: if the stack that is used
to store the operator is empty, then just push the operator
to the 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

• If the operator on TOS (Top Of Stack) is lower than the


operator read from infix string then push operator read to
stack, because we cannot process the lower operator
yet, and we cannot also process the higher operator yet
– just in case there is more higher hierarchy operator in
the next reading)

18
3rd B Rule

If the operator on TOS is greater or equal to the operator read


from infix string then:
• pop all operators from the stack while they have higher or
equal hierarchy than the operator from infix string
o put these operators in the postfix string
• push the operator read from string to the stack

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

Iteration-7 No More Item in Infix


• 5 is operand (1st Rule), then • 4th Rule : pop all items from
write to Postfix stack and put them in the postfix
• Postfix = 2 3 4 / 5 • Postfix = 2 3 4 / 5 * -
• Stack = - * (tos) • Stack = <empty>

24
What if Infix contains brackets?
5th Rule: Including the brackets

• 5th rule: include the opening bracket (5th A rule) and


closing bracket (5th B rule) in your expression. What
should you do if you encounter them?

• 5th A rule : For the opening bracket, just push it directly to


the stack

Note: opening bracket is lowest hierarchy

26
5th B Rule

If the item read is closing bracket then:


• Pop operator from the stack and put it in the postfix
• Do this process continuously until the item popped is
opening bracket (no need to put the opening bracket in
the postfix)

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)

Iteration-7 No more item in infix


• ) is closing bracket (5th B Rule), • 4th Rule: pop all items from
then Pop operator from the stack and put them in the
stack and put it in the postfix postfix
until opening bracket • Postfix = 2 3 4 - *
• Postfix = 2 3 4 - • Stack = <empty>
• Stack = * (tos)

31
Questions???
Exercise

1. Illustrate / simulate the conversion of Infix expression to


Postfix expression.
Infix : ( 2 + 3 ) * ( 4 - 5 )
2. After obtaining Postfix expression from point number 1, also
illustrate / simulate Postfix calculations.

33

You might also like