Lect05 - Queues
Lect05 - Queues
Queues Lecture 05
Outline
• Queue ADT
– Introduction
– Operations
– Examples
– Queue interfaces
• Our own Queue interface
• java.util.Queue interface
• Queue data structure
– Array-based implementation
– Singly-linked list implementation
• Applications
Introduction
• A queue is a collection of objects that are inserted and
removed according to the first-in, first-out (FIFO)
principle.
• Elements enter a queue at the back and are removed
from the front.
Queue ADT
Formally, the queue ADT defines a collection that keeps objects in a sequence,
where:
• Access and deletion are restricted to the first element in the queue.
• Insertion is restricted to the last of the queue.
Operations:
Examples
1. enqueue(5) front (back)
5
Examples
2. enqueue(3) back front
3 5
Examples
3. size() back front
3 5
returns 2
Examples
4. dequeue() front (back)
3
returns 5
Examples
5. isEmpty() front (back)
3
returns false
Examples
6. dequeue() front (back)
returns 3
Examples
7. isEmpty() front (back)
returns true
Examples
8. dequeue() front (back)
returns null
Examples
9. equeue(7) front (back)
7
Examples
10. equeue(9) back front
9 7
Examples
11. first() back front
9 7
returns 7
Examples
11. enqueue(4) back front
4 9 7
Our Queue Interface
java.util.Queue Interface