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.

3.6 Exercises<br />

For source code <strong>and</strong> help with exercises, please visit<br />

java.datastructures.net.<br />

Re<strong>in</strong>forcement<br />

R-3.1<br />

The add <strong>and</strong> remove methods of Code Fragments 3.3 <strong>and</strong> 3.4 do not keep<br />

track of the number,n, of non-null entries <strong>in</strong> the array, a. Instead, the unused<br />

cells po<strong>in</strong>t to the null object. Show how to change these methods so that they<br />

keep track of the actual size of a <strong>in</strong> an <strong>in</strong>stance variable n.<br />

R-3.2<br />

Describe a way to use recursion to add all the elements <strong>in</strong> a n × n (two<br />

dimensional) array of <strong>in</strong>tegers.<br />

R-3.3<br />

Expla<strong>in</strong> how to modify the Caesar cipher program (Code Fragment 3.9) so that<br />

it performs ROT 13 encryption <strong>and</strong> decryption, which uses 13 as the alphabet<br />

shift amount. How can you further simplify the code so that the body of the<br />

decrypt method is only a s<strong>in</strong>gle l<strong>in</strong>e?<br />

R-3.4<br />

Expla<strong>in</strong> the changes that would have be made to the program of Code Fragment<br />

3.9 so that it could perform the Caesar cipher for messages that are written <strong>in</strong> an<br />

alphabet-based language other than English, such as Greek, Russian, or Hebrew.<br />

R-3.5<br />

What is the exception that is thrown when advance or remove is called on an<br />

empty list, from Code Fragment 3.25? Expla<strong>in</strong> how to modify these methods so<br />

that they give a more <strong>in</strong>structive exception name for this condition.<br />

R-3.6<br />

Give a recursive def<strong>in</strong>ition of a s<strong>in</strong>gly l<strong>in</strong>ked list.<br />

R-3.7<br />

Describe a method for <strong>in</strong>sert<strong>in</strong>g an element at the beg<strong>in</strong>n<strong>in</strong>g of a s<strong>in</strong>gly l<strong>in</strong>ked<br />

list. Assume that the list does not have a sent<strong>in</strong>el header node, <strong>and</strong> <strong>in</strong>stead uses<br />

a variable head to reference the first node <strong>in</strong> the list.<br />

205

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

Saved successfully!

Ooh no, something went wrong!