23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Another fundamental data structure is the queue. It is a close "cous<strong>in</strong>" of the stack, as<br />

a queue is a collection of objects that are <strong>in</strong>serted <strong>and</strong> removed accord<strong>in</strong>g to the first<strong>in</strong><br />

first-out (FIFO) pr<strong>in</strong>ciple. That is, elements can be <strong>in</strong>serted at any time, but only<br />

the element that has been <strong>in</strong> the queue the longest can be removed at any time.<br />

We usually say that elements enter a queue at the rear <strong>and</strong> are removed from the<br />

front. The metaphor for this term<strong>in</strong>ology is a l<strong>in</strong>e of people wait<strong>in</strong>g to get on an<br />

amusement park ride. People wait<strong>in</strong>g for such a ride enter at the rear of the l<strong>in</strong>e <strong>and</strong><br />

get on the ride from the front of the l<strong>in</strong>e.<br />

5.2.1 The Queue Abstract <strong>Data</strong> Type<br />

Formally, the queue abstract data type def<strong>in</strong>es a collection that keeps objects <strong>in</strong> a<br />

sequence, where element access <strong>and</strong> deletion are restricted to the first element <strong>in</strong> the<br />

sequence, which is called the front of the queue, <strong>and</strong> element <strong>in</strong>sertion is restricted<br />

to the end of the sequence, which is called the rear of the queue. This restriction<br />

enforces the rule that items are <strong>in</strong>serted <strong>and</strong> deleted <strong>in</strong> a queue accord<strong>in</strong>g to the<br />

first-<strong>in</strong> first-out (FIFO) pr<strong>in</strong>ciple.<br />

The queue abstract data type (ADT) supports the follow<strong>in</strong>g two fundamental<br />

methods:<br />

enqueue(e): Insert element e at the rear of the queue.<br />

dequeue(): Remove <strong>and</strong> return from the queue the object at the front;<br />

an error occurs if the queue is empty.<br />

Additionally, similar to the case with the Stack ADT, the queue ADT <strong>in</strong>cludes the<br />

follow<strong>in</strong>g support<strong>in</strong>g methods:<br />

size(): Return the number of objects <strong>in</strong> the queue.<br />

empty.<br />

isEmpty(): Return a Boolean value that <strong>in</strong>dicates whether the queue is<br />

front(): Return, but do not remove, the front object <strong>in</strong> the queue; an<br />

error occurs if the queue is empty.<br />

Example 5.4: The follow<strong>in</strong>g table shows a series of queue operations <strong>and</strong> their<br />

effects on an <strong>in</strong>itially empty queue Q of <strong>in</strong>teger objects. For simplicity, we use<br />

<strong>in</strong>tegers <strong>in</strong>stead of <strong>in</strong>teger objects as arguments of the operations.<br />

Operation<br />

Output<br />

front ← Q ← rear<br />

285

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!