Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

IT2153/IT2352/IT2553/IT2653/IT2852

Data Structures & Algorithms


Topic 08: Stacks
version 1.0

(Content adapted from – Data Structures & Algorithms using Python.


Rance D. Necaise, Wiley, 1st Edition, 2011.)

Topic 08: Stacks AY2022/23S1


Stacks
 What is a Stack?
 Stack ADT
 Stack Implementation (with a Python
List)
 Stack Application
 Prefix / Infix / Postfix Expressions
 Evaluating Postfix Expressions

Topic 08: Stacks AY2022/23S1


What is a Stack?
 Stack is a linear data structure that has
restrictions on how items are added to
and removed from it.
 The last item to be added is the first item to
be removed
 Known as last-in, first-out (LIFO)
 Push – add an item to a stack
 Pop – remove an item from a stack
 Peek or Top – return the top item of a stack

Topic 08: Stacks AY2022/23S1


What is a Stack?
A push (add) B push

Top of
Top of A
stack
stack

pop (remove)

Top of
stack B Top of
A stack A

A stack is a data structure of ordered items where items can


be added and removed only at one end (called the top).

Topic 08: Stacks AY2022/23S1


The Stack ADT
 ADT – Abstract Data Types
 A type (or class) of objects whose behaviour
is defined by a set of values and a set of
operations.

 E.g. Stack ADT, Queue ADT, Tree ADT etc.

5
Topic 08: Stacks AY2022/23S1
6
Topic 08: Stacks AY2022/23S1
Stack Implementation (with a Python List)

# Implementation of the Stack ADT using a Python list.


class Stack :
# Creates an empty stack.
def __init__( self ):
self._theItems = list()

# Returns True if the stack is empty or False


# otherwise.
def isEmpty( self ):

# Returns the number of items in the stack.


def __len__ ( self ):

Topic 08: Stacks AY2022/23S1


Stack Implementation (with a Python List)

# Returns the top item on the stack without


# removing it.
def peek( self ):
assert not self.isEmpty(), \
"Cannot peek at an empty stack"

# Removes and returns the top item on the stack.


def pop( self ):
assert not self.isEmpty(), \
"Cannot pop from an empty stack"

# Push an item onto the top of the stack.


def push( self, item ):

Topic 08: Stacks AY2022/23S1


Evaluating Mathematical
Expressions

 How would you evaluate the following


mathematical expression?
A * B + C / D

Python’s Operator 1st: A * B


Precedence:
2nd: C / D
https://docs.python.or
g/3/reference/expressi
3rd: Concluding
ons.html with the addition
11
Topic 08: Stacks AY2022/23S1
Evaluating Mathematical
Expressions

 To simplify the evaluation of a


mathematical expression for a computer
program, express it in another form:

 Instead of:
 A * B + C / D  Infix
 We represent it as:
 A B * C D / +  Postfix
12
Topic 08: Stacks AY2022/23S1
Prefix / Infix / Postfix Expressions

 There are three different notations to


represent a mathematical expressions

Prefix - operator appears before its operands


(e.g. + 2 3).
Infix - operator appears in between its operands
(e.g. 2 + 3).
Postfix - operator appears after its operands
(e.g. 2 3 +)

13
Topic 08: Stacks AY2022/23S1
Prefix / Infix / Postfix Expressions

Prefix Expression Infix Expression Postfix Expression

+ab a+b ab+

-+abc a+b-c ab+c-

*+ab-cd (a + b) * (c - d) ab+cd-*

Advantages: Prefix and Postfix need not use parentheses


to override order of precedence and both create expressions
in unique form.
14
Topic 08: Stacks AY2022/23S1
Converting from Infix to Postfix
 Convert A * B + C / D to Postfix
Step 1: Place parentheses around every group of
operators in the correct order of evaluation:
((A * B) + (C / D))

Step 2: For each set of parentheses, move the


operator from the middle to the end preceding the
corresponding closing parenthesis:
((A B *)(C D /) +)
Step 3: Remove all of the parentheses:
A B * C D / +
15
Topic 08: Stacks AY2022/23S1
Converting from Infix to Postfix
 Convert A * (B + C) / D to Postfix
Step 1: Place parentheses around every group of
operators in the correct order of evaluation:
((A * (B + C)) / D)

Step 2: For each set of parentheses, move the


operator from the middle to the end preceding the
corresponding closing parenthesis:
((A (B C +) *) D /)
Step 3: Remove all of the parentheses:
A B C + * D /
16
Topic 08: Stacks AY2022/23S1
Infix to Prefix
 What about converting from Infix to
Prefix notation?
 Apply similar steps except that the
operator is moved to the front of each set
of parentheses.

Refer to Tutorial Questions &


Past-Year Exam Questions.

Topic 08: Stacks AY2022/23S1


Evaluating Postfix Expressions
 Scan the expression, one character (or token)
at a time. For each token:
 Algorithm:
1) If the current item is an operand, push its value
onto the stack.
2) If the current item is an operator:
a) Pop the top two operands off the stack.
b) Perform the operation (top value is the right operand).
c) Push the result of this operation back onto the stack

 The final result of the expression is the last


value of the stack.
Topic 08: Stacks AY2022/23S1
Evaluating Postfix Expressions

A=8 C=3
B=2 D=4

Topic 08: Stacks AY2022/23S1


Evaluating Postfix Expressions

A=8 C=3
B=2 D=4

Topic 08: Stacks AY2022/23S1


Past-Year Exam Question
 Trace and evaluate the postfix expression:
20 6 * 8 2 - / 4 * 15 -

Expression: 20 6 * 8 2 - / 4 * 15 -

2
Stack
6 8 8 6 4 15

20 20 120 120 120 120 20 20 80 80 65

Topic 08: Stacks AY2022/23S1

You might also like