0% found this document useful (0 votes)
38 views3 pages

Elem Dat Astruc T Queues

The document discusses the queue data structure and its operations. A queue follows a first-in, first-out (FIFO) approach to inserting and removing items. Main queue operations include enqueue to insert an item at the rear and dequeue to remove an item from the front. The document provides examples of queue operations and implementations using arrays.

Uploaded by

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

Elem Dat Astruc T Queues

The document discusses the queue data structure and its operations. A queue follows a first-in, first-out (FIFO) approach to inserting and removing items. Main queue operations include enqueue to insert an item at the rear and dequeue to remove an item from the front. The document provides examples of queue operations and implementations using arrays.

Uploaded by

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

The Queue ADT

The Queue ADT stores arbitrary Auxiliary queue


objects operations:
Queues Insertions and deletions follow
the first-in first-out scheme
object front(): returns the
element at the front without
Insertions are at the rear of the removing it
queue and removals are at the integer size(): returns the

front of the queue number of elements stored


boolean isEmpty(): indicates
Main queue operations:
whether no elements are
enqueue(object): inserts an
stored
element at the end of the
queue Exceptions
object dequeue(): removes and Attempting the execution of
returns the element at the front dequeue or front on an
of the queue empty queue throws an
EmptyQueueException
2010 Goodrich, Tamassia Queues 1 2010 Goodrich, Tamassia Queues 2

Example Applications of Queues


Operation Output Q
enqueue(5) (5)
enqueue(3) (5, 3) Direct applications
dequeue() 5 (3)
enqueue(7) (3, 7) Waiting lists, bureaucracy
dequeue() 3 (7)
Access to shared resources (e.g., printer)
front() 7 (7)
dequeue() 7 () Multiprogramming
dequeue() error ()
isEmpty() true () Indirect applications
enqueue(9) (9)
enqueue(7) (9, 7) Auxiliary data structure for algorithms
size()
enqueue(3)
2

(9, 7)
(9, 7, 3)
Component of other data structures
enqueue(5) (9, 7, 3, 5)
dequeue() 9 (7, 3, 5)
2010 Goodrich, Tamassia Queues 3 2010 Goodrich, Tamassia Queues 4
Array-based Queue Queue Operations
Use an array of size N in a circular fashion

We use the Algorithm size()
Two variables keep track of the front and rear return (N - f + r) mod N
modulo operator

f index of the front element
r index immediately past the rear element (remainder of
Algorithm isEmpty()
Array location r is kept empty division) return (f = r)
normal configuration
Q Q
0 1 2 f r 0 1 2 f r
wrapped-around configuration Q
Q 0 1 2 r f
0 1 2 r f
2010 Goodrich, Tamassia Queues 5 2010 Goodrich, Tamassia Queues 6

Queue Operations (cont.) Queue Operations (cont.)


Operation enqueue Algorithm enqueue(o) Operation dequeue Algorithm dequeue()
throws an exception if if size() = N - 1 then throws an exception if isEmpty() then
the array is full throw FullQueueException if the queue is empty throw EmptyQueueException
This exception is else This exception is else
implementation- Q[r] o specified in the o Q[f]
dependent r (r + 1) mod N queue ADT f (f + 1) mod N
return o
Q
0 1 2 f r Q
0 1 2 f r
Q
0 1 2 r f Q
0 1 2 r f
2010 Goodrich, Tamassia Queues 7 2010 Goodrich, Tamassia Queues 8
Queue Interface in Java Application: Round Robin Schedulers
public interface Queue<E> { We can implement a round robin scheduler using a
Java interface queue Q by repeatedly performing the following
corresponding to public int size(); steps:
our Queue ADT public boolean isEmpty(); 1. e = Q.dequeue()
Service element e
Requires the public E front()
2.

Q.enqueue(e)
definition of class throws EmptyQueueException;
3.
Queue
EmptyQueueException public void enqueue(E element); Dequeue Enqueue
No corresponding public E dequeue()
built-in Java class throws EmptyQueueException; Shared
} Service

2010 Goodrich, Tamassia Queues 9 2010 Goodrich, Tamassia Queues 10

You might also like