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.

Th<strong>in</strong>k about how to extend the circular array implementation of the queue ADT<br />

given <strong>in</strong> the previous chapter.)<br />

C-6.10<br />

Describe an efficient way of putt<strong>in</strong>g an array list represent<strong>in</strong>g a deck of n cards<br />

<strong>in</strong>to r<strong>and</strong>om order. You may use a function, r<strong>and</strong>omInteger(n), which<br />

returns a r<strong>and</strong>om number between 0 <strong>and</strong> n − 1, <strong>in</strong>clusive. Your method should<br />

guarantee that every possible order<strong>in</strong>g is equally likely. What is the runn<strong>in</strong>g<br />

time of your method?<br />

C-6.11<br />

Describe a method for ma<strong>in</strong>ta<strong>in</strong><strong>in</strong>g a favorites list L such that every element <strong>in</strong> L<br />

has been accessed at least once <strong>in</strong> the last n accesses, where n is the size of L.<br />

Your scheme should add only O(1) additional amortized time to each operation.<br />

C-6.12<br />

Suppose we have an n-element list L ma<strong>in</strong>ta<strong>in</strong>ed accord<strong>in</strong>g to the move-to-front<br />

heuristic. Describe a sequence of n 2 accesses that is guaranteed to take Ω(n 3 )<br />

time to perform on L.<br />

C-6.13<br />

Design a circular node list ADT that abstracts a circularly l<strong>in</strong>ked list <strong>in</strong> the -<br />

same way that the node list ADT abstracts a doubly l<strong>in</strong>ked list.<br />

C-6.14<br />

Describe how to implement an iterator for a circularly l<strong>in</strong>ked list. S<strong>in</strong>ce<br />

hasNext() will always return true <strong>in</strong> this case, describe how to perform<br />

hasNewNext(), which returns true if <strong>and</strong> only if the next node <strong>in</strong> the list<br />

has not previously had its element returned by this iterator.<br />

C-6.15<br />

Describe a scheme for creat<strong>in</strong>g list iterators that fail fast, that is, they all<br />

become <strong>in</strong>valid as soon as the underly<strong>in</strong>g list changes.<br />

C-6.16<br />

An array is sparse if most of its entries are null. A list L can be used to<br />

implement such an array, A, efficiently. In particular, for each nonnull cell A[i],<br />

we can store an entry (i, e) <strong>in</strong> L, where e is the element stored at A[i]. This<br />

approach allows us to represent A us<strong>in</strong>g O(m) storage, where m is the number of<br />

nonnull entries <strong>in</strong> A. Describe <strong>and</strong> analyze efficient ways of perform<strong>in</strong>g the<br />

369

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

Saved successfully!

Ooh no, something went wrong!