11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

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.

Sec. 4.3 Queues 125/** Queue ADT */public interface Queue {}/** Reinitialize the queue. The user is responsible forreclaiming the storage used by the queue elements. */public void clear();/** Place an element <strong>at</strong> the rear of the queue.@param it The element being enqueued. */public void enqueue(E it);/** Remove <strong>and</strong> return element <strong>at</strong> the front of the queue.@return The element <strong>at</strong> the front of the queue. */public E dequeue();/** @return The front element. */public E frontValue();/** @return The number of elements in the queue. */public int length();Figure 4.24 The Java ADT for a queue.Recursive algorithms lend themselves to efficient implement<strong>at</strong>ion with a stackwhen the amount of inform<strong>at</strong>ion needed to describe a sub-problem is small. Forexample, Section 7.5 discusses a stack-based implement<strong>at</strong>ion for Quicksort.4.3 QueuesLike the stack, the queue is a list-like structure th<strong>at</strong> provides restricted access toits elements. Queue elements may only be inserted <strong>at</strong> the back (called an enqueueoper<strong>at</strong>ion) <strong>and</strong> removed from the front (called a dequeue oper<strong>at</strong>ion). Queues oper<strong>at</strong>elike st<strong>and</strong>ing in line <strong>at</strong> a movie the<strong>at</strong>er ticket counter. 1 If nobody che<strong>at</strong>s, thennewcomers go to the back of the line. The person <strong>at</strong> the front of the line is the nextto be served. Thus, queues release their elements in order of arrival. Accountantshave used queues since long before the existence of computers. They call a queuea “FIFO” list, which st<strong>and</strong>s for “First-In, First-Out.” Figure 4.24 shows a samplequeue ADT. This section presents two implement<strong>at</strong>ions for queues: the array-basedqueue <strong>and</strong> the linked queue.4.3.1 Array-Based QueuesThe array-based queue is somewh<strong>at</strong> tricky to implement effectively. A simple conversionof the array-based list implement<strong>at</strong>ion is not efficient.1 In Britain, a line of people is called a “queue,” <strong>and</strong> getting into line to wait for service is called“queuing up.”

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

Saved successfully!

Ooh no, something went wrong!