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.

R-6.16<br />

Describe how to create an iterator for a node list that returns every other<br />

element <strong>in</strong> the list.<br />

R-6.17<br />

Suppose we are ma<strong>in</strong>ta<strong>in</strong><strong>in</strong>g a collection C of elements such that, each time we<br />

add a new element to the collection, we copy the contents of C <strong>in</strong>to a new array<br />

list of just the right size. What is the runn<strong>in</strong>g time of add<strong>in</strong>g n elements to an<br />

<strong>in</strong>itially empty collection C <strong>in</strong> this case?<br />

R-6.18<br />

Describe an implementation of the methods addLast <strong>and</strong> add Before<br />

realized by us<strong>in</strong>g only methods <strong>in</strong> the set {isEmpty, checkPosition,<br />

first, last, prev, next, addAfter, addFirst}.<br />

R-6.19<br />

Let L be ma<strong>in</strong>ta<strong>in</strong>ed to be a list of n items ordered by decreas<strong>in</strong>g access count.<br />

Describe a series of O(n 2 ) accesses that will reverse L.<br />

R-6.20<br />

Let L be a list of n items ma<strong>in</strong>ta<strong>in</strong>ed accord<strong>in</strong>g to the move-to-front heuristic.<br />

Describe a series of O(n) accesses that will reverse L.<br />

Creativity<br />

C-6.1<br />

Give pseudo-code for the methods of a new class, Shr<strong>in</strong>k<strong>in</strong>gArrayList,<br />

that extends the class ArrayIndexList shown <strong>in</strong> Code Fragment 6.3 <strong>and</strong><br />

adds a method, shr<strong>in</strong>kToFit(), which replaces the underly<strong>in</strong>g array with an<br />

array whose capacity is exactly equal to the number of elements currently <strong>in</strong> the<br />

array list.<br />

C-6.2<br />

Describe what changes need to be made to the extendable array implementation<br />

given <strong>in</strong> Code Fragment 6.3 <strong>in</strong> order to shr<strong>in</strong>k by half the size N of the array any<br />

time the number of elements <strong>in</strong> the array list goes below N/4.<br />

C-6.3<br />

367

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

Saved successfully!

Ooh no, something went wrong!