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-3.8<br />

Give an algorithm for f<strong>in</strong>d<strong>in</strong>g the penultimate node <strong>in</strong> a s<strong>in</strong>gly l<strong>in</strong>ked list where<br />

the last element is <strong>in</strong>dicated by a null next reference.<br />

R-3.9<br />

Describe a nonrecursive method for f<strong>in</strong>d<strong>in</strong>g, by l<strong>in</strong>k hopp<strong>in</strong>g, the middle node<br />

of a doubly l<strong>in</strong>ked list with header <strong>and</strong> trailer sent<strong>in</strong>els. (Note: This method<br />

must only use l<strong>in</strong>k hopp<strong>in</strong>g; it cannot use a counter.) What is the runn<strong>in</strong>g time<br />

of this method?<br />

R-3.10<br />

Describe a recursive algorithm for f<strong>in</strong>d<strong>in</strong>g the maximum element <strong>in</strong> an array A<br />

of n elements. What is your runn<strong>in</strong>g time <strong>and</strong> space usage?<br />

R-3.11<br />

Draw the recursion trace for the execution of method ReverseArray (A ,0,4)<br />

(Code Fragment 3.32) on array A = {4,3,6,2,5}.<br />

R-3.12<br />

Draw the recursion trace for the execution of method PuzzleSolve(3,S, U)<br />

(Code Fragment 3.37), where S is empty <strong>and</strong> U = {a,b,c,d}.<br />

R-3.13<br />

Write a short <strong>Java</strong> method that repeatedly selects <strong>and</strong> removes a r<strong>and</strong>om entry<br />

from an array until the array holds no more entries.<br />

R-3.14<br />

Write a short <strong>Java</strong> method to count the number of nodes <strong>in</strong> a circularly l<strong>in</strong>ked<br />

list.<br />

Creativity<br />

C-3.1<br />

Give <strong>Java</strong> code for perform<strong>in</strong>g add(e) <strong>and</strong> remove(i) methods for game<br />

entries, stored <strong>in</strong> an array a, as <strong>in</strong> Code Fragments 3.3 <strong>and</strong> 3.4, except now don't<br />

ma<strong>in</strong>ta<strong>in</strong> the game entries <strong>in</strong> order. Assume that we still need to keep n entries<br />

stored <strong>in</strong> <strong>in</strong>dices 0 to n − 1. Try to implement the add <strong>and</strong> remove methods<br />

without us<strong>in</strong>g any loops, so that the number of steps they perform does not<br />

depend on n.<br />

206

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

Saved successfully!

Ooh no, something went wrong!