0% found this document useful (0 votes)
23 views28 pages

Lect05 - Queues

This document discusses queues and their implementation using arrays and linked lists. It defines queues as first-in, first-out data structures with enqueue and dequeue operations. Common queue operations like size, isEmpty, and first are presented. Array-based queues use wrap-around techniques while linked list queues align the front with the head and back with the tail of the list. Examples of queue applications include processing customer requests, calls to service centers, and computing tasks like printer jobs and web server requests.

Uploaded by

rahafpro2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views28 pages

Lect05 - Queues

This document discusses queues and their implementation using arrays and linked lists. It defines queues as first-in, first-out data structures with enqueue and dequeue operations. Common queue operations like size, isEmpty, and first are presented. Array-based queues use wrap-around techniques while linked list queues align the front with the head and back with the tail of the list. Examples of queue applications include processing customer requests, calls to service centers, and computing tasks like printer jobs and web server requests.

Uploaded by

rahafpro2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

CS202

Data Structures & Algorithms

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

Double Ended Queue


(supports element
insertion and removal
at both ends)
java.util.Queue Interface
add() and offer()
• offer() inserts an element if possible,
otherwise returning false.
• This differs from add(), which can fail to add
an element only by throwing an unchecked
exception.
• offer() is designed for use when failure is a
normal, rather than exceptional occurrence, for
example, in fixed-capacity (or "bounded")
queues.

remove() and poll()


• The remove() and poll() methods remove
and return the head of the queue.
• If the queue is empty, remove() throws an
exception, while poll() returns null.
Array-based Queue Implementation
• enqueue() is straight-forward to implement and takes to implement.
• A straight-forward implementation of dequeue() requires removing the
first element and shifting, which would take steps.
• Use a wrap-around technique in the implementation of enqueue() and
dequeue().
– Maintain a variable to keep track of the index of the first element.
– enqueue() a new element at index .
– dequeue() sets .
– No shifting is required and the complexity becomes .
Queue Class
Class Declaration and Constructors
Queue Class
Methods: size and isEmpty
Queue Class
Method: enqueue
Queue Class
Method: first
Queue Class
Method: dequeue
Singly-linked List Queue Implementation
Align the
• front of the queue with the head of the list.
• back of the queue with the tail of the list.
This is because the only update operation that singly linked lists efficiently support at the
back end is an insertion.

dequeue() calls enqueue() calls


removeFirst() addLast()
LinkedQueue Class
Applications
There are many other applications of
queues.
Processing customer requests in:
– Stores
– Purchasing tickets (a)
– Reservation centers
Handling:
– Calls to a customer service center
(b)
– Wait-list at a restaurant
In Computing:
– Networked printer
– Web server responding to requests

You might also like