5
Data Structures - Queues

Data Structures - Queues. Introducing the Queue FIFO ~ First In First Out How does a queue (line) work? 1. First person gets in line 2. Second person

Embed Size (px)

Citation preview

Page 1: Data Structures - Queues. Introducing the Queue FIFO ~ First In First Out How does a queue (line) work? 1. First person gets in line 2. Second person

Data Structures - Queues

Page 2: Data Structures - Queues. Introducing the Queue FIFO ~ First In First Out How does a queue (line) work? 1. First person gets in line 2. Second person

Introducing the Queue FIFO ~ First In First Out How does a queue (line) work?

1. First person gets in line2. Second person gets in line after first person3. etc.4. The first person to get out of the line is the first person in

line enqueue – data is added to the end (tail) of the

queue dequeue – data is removed from the beginning

(head) of the queue isEmpty – returns True if queue is empty

Page 3: Data Structures - Queues. Introducing the Queue FIFO ~ First In First Out How does a queue (line) work? 1. First person gets in line 2. Second person

Queue Example

ENQUEUE D

ENQUEUE EENQUEUE A #What is at the head of the line? and the

tail?

DEQUEUEDEQUEUE #What is at the head of the line? and the tail?

Page 4: Data Structures - Queues. Introducing the Queue FIFO ~ First In First Out How does a queue (line) work? 1. First person gets in line 2. Second person

Why do we use queues? Used to ensure the first-come-first-serve

concept The first things added are the first things to

be removed Where do we see queues in real life? Where do you think we see queues in

technology?

Page 5: Data Structures - Queues. Introducing the Queue FIFO ~ First In First Out How does a queue (line) work? 1. First person gets in line 2. Second person

Queue Implementation How do we implement a queue?

class Static size vs. Dynamic size Let’s draw a queue...

Try out some queue operations (enqueue, dequeue, etc.) What pieces of information do we need to track?