You are on page 1of 39

Material adapted from: CS370

CS2001 – DATA STRUCTURES

Lecture # 09
September 18, 2023 Dr. Rabia Maqsood
Fall 2023 rabia.maqsood@nu.edu.pk
FAST – NUCES, CFD Campus
TODAY’S TOPICS
q Applications of Stack (LIFO)
q Parenthesis matching
q Function calls
q Postfix calculator (a.k.a reverse polish)

q Recursion: basics

CS 2001 - FALL 2023 2


PROBLEMS THAT USE STACKS
The runtime stack used by a process (running program) to keep
track of methods in progress
­ Search problems
­ Undo, redo, back, forward

CS 2001 - FALL 2023 3


BALANCED SYMBOL CHECKING
In processing programs and working with computer languages there are many
instances when symbols must be balanced
{},[],()

A stack is useful for checking symbol balance. When a closing symbol is found it must match the most
recent opening symbol of the same type.

Algorithm?

CS 2001 - FALL 2023 4


ALGORITHM FOR BALANCED SYMBOL CHECKING
Make an empty stack
read symbols until end of file
­ if the symbol is an opening symbol push it onto the stack
­ if it is a closing symbol do the following
­ if the stack is empty report an error
­ otherwise pop the stack. If the symbol popped does not match the closing symbol report an error

At the end of the file if the stack is not empty report an error

CS 2001 - FALL 2023 5


FUNCTION CALLS
This next example discusses function calls

In Assembly language course, you will see how stacks are implemented in
hardware on all CPUs to facilitate function calling

The simple features of a stack indicate why almost all programming


languages are based on function calls

CS 2001 - FALL 2023 6


FUNCTION
CALLS
You write a function to solve
a problem
­ the function may require sub-
problems to be solved, hence, it
may call another function

­ Once a function is finished, it


returns to the function which
called it

CS 2001 - FALL 2023 7


FUNCTION CALLS
You will notice that the when a function returns, execution and the return
value is passed back to the last function which was called

This is again, the last-in—first-out property

CS 2001 - FALL 2023 8


MATHEMATICAL CALCULATIONS
What is 3 + 2 * 4? 2 * 4 + 3? 3 * 2 + 4?
The precedence of operators affects the order of operations. A mathematical
expression cannot simply be evaluated left to right.
A challenge when evaluating a program.
­ Lexical analysis is the process of interpreting a program.
­ Involves Tokenization

What about 1 - 2 - 4 ^ 5 * 3 * 6 / 7 ^ 2 ^ 3

CS 2001 - FALL 2023 9


INFIX AND POSTFIX EXPRESSIONS
The way we are used to write arithmetic expressions is known as infix
notation
­ An operator comes within the two operands: 3 * 2 + 1
­ Requires parenthesis to convey the exact meaning
­ Multiple scans are required to evaluate an expression

Example:
(3 + 4) × 5 – 6 = 29
3+4 × 5–6 = 17
3 + 4 × (5 – 6) = –1
(3 + 4) × (5 – 6) = –7

CS 2001 - FALL 2023 10


REVERSE-POLISH NOTATION
Postfix expression in which an operator comes after the operands
­ Does not require any precedence rules
­ Only a single scan will evaluate the expression

Example: 3 2 * 1 + is postfix of 3 * 2 + 1

Postfix notation is also known as reverse-polish notation

CS 2001 - FALL 2023 12


REVERSE-POLISH NOTATION
This is called reverse-Polish notation after the mathematician
Jan Łukasiewicz

He also made significant contributions to logic and other fields

http://www.audiovis.nac.gov.pl/

CS 2001 - FALL 2023 13


REVERSE-POLISH NOTATION
Place the operands first, followed by the operator:
(3 + 4) × 5 – 6
3 4 + 5 × 6 –

Parsing reads left-to-right and performs any operation on the last two operands:
3 4 + 5 × 6 –
7 5 × 6 –
35 6 –
29

CS 2001 - FALL 2023 14


REVERSE-POLISH NOTATION
Other examples:
3 4 5 × + 6 –
3 20 + 6 –
23 6 –
17
3 4 5 6 – × +
3 4 –1 × +
3 –4 +
–1
CS 2001 - FALL 2023 15
REVERSE-POLISH NOTATION
Benefits:
­ No ambiguity and no brackets are required
­ It is the same process used by a computer to perform computations:
­ operands must be loaded into registers before operations can be performed on them

­ Reverse-Polish can be processed using stacks

CS 2001 - FALL 2023 16


REVERSE-POLISH NOTATION
The easiest way to parse reverse-Polish notation is to use an operand stack:
­ operands are processed by pushing them onto the stack
­ when processing an operator:
­ pop the last two items off the operand stack,
­ perform the operation, and
­ push the result back onto the stack

CS 2001 - FALL 2023 17


REVERSE-POLISH NOTATION
Evaluate the following reverse-Polish expression using a stack:
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

CS 2001 - FALL 2023 18


REVERSE-POLISH NOTATION
Push 1 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

1
CS 2001 - FALL 2023 19
REVERSE-POLISH NOTATION
Push 1 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

2
1
CS 2001 - FALL 2023 20
REVERSE-POLISH NOTATION
Push 3 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

3
2
1
CS 2001 - FALL 2023 21
REVERSE-POLISH NOTATION
Pop 3 and 2 and push 2 + 3 = 5
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

5
1
CS 2001 - FALL 2023 22
REVERSE-POLISH NOTATION
Push 4 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

4
5
1
CS 2001 - FALL 2023 23
REVERSE-POLISH NOTATION
Push 5 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

5
4
5
1
CS 2001 - FALL 2023 24
REVERSE-POLISH NOTATION
Push 6 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

6
5
4
5
1
CS 2001 - FALL 2023 25
REVERSE-POLISH NOTATION
Pop 6 and 5 and push 5 × 6 = 30
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

30
4
5
1
CS 2001 - FALL 2023 26
REVERSE-POLISH NOTATION
Pop 30 and 4 and push 4 – 30 = –26
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

–26
5
1
CS 2001 - FALL 2023 27
REVERSE-POLISH NOTATION
Push 7 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

7
–26
5
1
CS 2001 - FALL 2023 28
REVERSE-POLISH NOTATION
Pop 7 and –26 and push –26 × 7 = –182
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

–182
5
1
CS 2001 - FALL 2023 29
REVERSE-POLISH NOTATION
Pop –182 and 5 and push –182 + 5 = –177
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

–177
1
CS 2001 - FALL 2023 30
REVERSE-POLISH NOTATION
Pop –177 and 1 and push 1 – (–177) = 178
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

178
CS 2001 - FALL 2023 31
REVERSE-POLISH NOTATION
Push 8 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

8
178
CS 2001 - FALL 2023 32
REVERSE-POLISH NOTATION
Push 1 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

9
8
178
CS 2001 - FALL 2023 33
REVERSE-POLISH NOTATION
Pop 9 and 8 and push 8 × 9 = 72
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

72
178
CS 2001 - FALL 2023 34
REVERSE-POLISH NOTATION
Pop 72 and 178 and push 178 + 72 = 250
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

250
CS 2001 - FALL 2023 35
REVERSE-POLISH NOTATION
Thus
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
evaluates to the value on the top: 250
The equivalent in-fix notation is
((1 – ((2 + 3) + ((4 – (5 × 6)) × 7))) + (8 × 9))

We can reduce the parentheses using order-of-operations:


1 – (2 + 3 + (4 – 5 × 6) × 7) + 8 × 9

CS 2001 - FALL 2023 36


EVALUATION OF POSTFIX EXPRESSIONS USING STACKS

Algorithm:
Given a proper postfix expression:
­ get the next token
­ if it is an operand push it onto the stack
­ else if it is an operator
­ pop the stack for the right hand operand
­ pop the stack for the left hand operand
­ apply the operator to the two operands
­ push the result onto the stack
­ when the expression has been exhausted the result is the top (and only element) of the stack

CS 2001 - FALL 2023 37


INFIX TO POSTFIX CONVERSION USING STACKS
Algorithm:
Given an infix expression:
1. Scan the expression from left to right
2. if the token is an operand, append it to the postfix expression
3. else if the token is an operator
3.1. If the precedence and associativity is greater than the operator on the stack,
3.1.1. push it onto the stack
3.2. else
3.2.1. pop all the operators from the stack with greater/equal precedence from the stack AND push the scanned operator onto the stack
4. else if the token is an “(“
4.1. push onto the stack
5. else if the token is an “)“
5.1. pop from the stack and append to the postfix expression until an “(“ is found, discard the parenthesis
6. Repeat steps 2-5 until the last token of the infix expression
7. Pop the stack and append each element to the postfix expression until the stack is empty

CS 2001 - FALL 2023 38


INFIX TO POSTFIX CONVERSION USING
STACKS
Example: 3 + 2 * 4
Postfix expression: 3 2 4 * +

Example: 6 * (5 + (2 + 3) * 8 + 3)

CS 2001 - FALL 2023 39


READING MATERIAL
Nell Dale: Chapter 4 & 7

CS 2001 - FALL 2023 41

You might also like