Professional Documents
Culture Documents
Learning Objectives
1. Understand the structure and operations of:
o Queues
o Stacks
2. Be able to implement the above data structures
2
RMIT Classification: Trusted
Queues
RMIT Classification: Trusted
4
RMIT Classification: Trusted
Queue Operations
5
RMIT Classification: Trusted
Queue Examples
• q1.enqueue(1)
What do the initially empty queues
• q1.enqueue(2)
- q1 and q2 – “look like” after the • q2.enqueue(3)
6
RMIT Classification: Trusted
• Example
o Hospital emergency room
7
RMIT Classification: Trusted
8
RMIT Classification: Trusted
Simulation Techniques
• A discrete-event simulation (DES) models the operation of a system as
a (discrete) sequence of events in time
o Each event occurs at a particular instant in time and marks a change of state
o Between consecutive events, no change in the system is assumed to occur; thus the
simulation time can directly jump to the occurrence time of the next event
• Generic Algorithm
10
RMIT Classification: Trusted
11
RMIT Classification: Trusted
12
RMIT Classification: Trusted
• We can move from a state <x1, y1> to several other states <x2, y2>,
<x3, y3>, <x4, y4>, etc.
• We can search this state space to find the path from <0, 0> to one of
the goal state <2, y> and <x, 2>
end while
14
RMIT Classification: Trusted
Stacks
RMIT Classification: Trusted
16
RMIT Classification: Trusted
o Path Finding
o Compilers
17
RMIT Classification: Trusted
18
RMIT Classification: Trusted
• Note that we could use a simpler solution - just use a counter that must never
go negative. However, what if we have more than one type of parentheses?
19
RMIT Classification: Trusted
Matching Parenthesis
• Extend the balanced braces solution so that the expression can contain 3 types
of delimiters (). {}, []
o Valid - abc[d(ef)gh]i{jk}
• In this case, we could use the same code as before, with one small modification
For each char in string
If char = {, (, or [ then stack.push {, ( or [ respectively
If char = }, ) or ] then
if stack.peek == a {, ( or [ respectively then pop else fail
20
RMIT Classification: Trusted
VTN-Air Flights
Adjacency List 21
RMIT Classification: Trusted
Infix Expressions
• Grammar that defines language of fully parenthesized infix
expression
Algebraic Expressions
• Infix - binary operator appears • No need for ( ) in prefix/postfix
between its operands - a + b o a+(b*c), + a * b c, a b c * +
• Prefix - operator appears before its o (a + b) * c, * + a b c, a b + c *
operands - + a b • Convert to prefix and postfix
• Postfix - operator appears after its o (a + b) * (c – d)
operands a b +. This is often called o 2+3+4
RPN or reverse polish notation
o (2+3) * 4
• Note that when converting, the o (2+3) * (5-4)
sequence of the operands is the
o ((2 + (6*3)) – (5+2))
same, just the operators need move
and parentheses are removed
24
RMIT Classification: Trusted
25
RMIT Classification: Trusted
Converting
a - (b + c * d) /e
26
RMIT Classification: Trusted
Stack Implementations
27
RMIT Classification: Trusted
28
RMIT Classification: Trusted
29
RMIT Classification: Trusted
30
RMIT Classification: Trusted