1 of 22

Queues�

Lecture 6

2 of 22

mic

3 of 22

Queues

4 of 22

The Queue ADT

A Queue is a collection of objects inserted and removed according to the

First In First Out (FIFO) principle. Think of a queue of people to Rubios.

5 of 22

Queue operations

Enqueue (insert) and Dequeue (remove) are the two main operations

6 of 22

Question

When using enqueue operation to place the following items in a queue:

enqueue(10)

enqueue(20)

enqueue(30)

enqueue(0)

enqueue(-30)

The output when dequeuing from the queue is:

7 of 22

Implementation. Arrays. O(1)

Main update methods:

  • Enqueue(e)
  • Dequeue( )

Additional useful methods

  • Peek(): Same as dequeue, but does not remove the element
  • Empty(): Boolean, True when the queue is empty
  • Size(): Returns the size of the queue

8 of 22

(Incorrect) Attempts to implement it

9 of 22

Regular array: dequeue

Select the correct code to delete from below:

A:

B:

C:

D: None of these are correct

10 of 22

Regular array: enqueue

Select the correct code to insert from below:

A:

B:

C:

D: None of these are correct

11 of 22

Issues

Dequeue:

12 of 22

Issues

Dequeue:

Enqueue:

13 of 22

Regular Array

14 of 22

15 of 22

16 of 22

Queues using circular arrays

17 of 22

Queues using circular arrays

18 of 22

Queues using circular arrays

19 of 22

Queues using circular arrays

20 of 22

Select the correct code to insert from below:

A:

B:

C:

D: None of these are correct

21 of 22

Select the correct code to insert from below:

A:

B:

C:

D: None of these are correct

22 of 22

Complexity of an Array based queues