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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

C-3.10<br />

Describe <strong>in</strong> detail an algorithm for revers<strong>in</strong>g a s<strong>in</strong>gly l<strong>in</strong>ked list L us<strong>in</strong>g only a<br />

constant amount of additional space <strong>and</strong> not us<strong>in</strong>g any recursion.<br />

C-3.11<br />

In the Towers of Hanoi puzzle, we are given a platform with three pegs, a, b,<br />

<strong>and</strong> c, stick<strong>in</strong>g out of it. On peg a is a stack of n disks, each larger than the next,<br />

so that the smallest is on the top <strong>and</strong> the largest is on the bottom. The puzzle is<br />

to move all the disks from peg a to peg c, mov<strong>in</strong>g one disk at a time, so that we<br />

never place a larger disk on top of a smaller one. See Figure 3.27 for an<br />

example of the case n = 4. Describe a recursive algorithm for solv<strong>in</strong>g the<br />

Towers of Hanoi puzzle for arbitrary n. (H<strong>in</strong>t: Consider first the subproblem of<br />

mov<strong>in</strong>g all but the nth disk from peg a to another peg us<strong>in</strong>g the third as<br />

"temporary storage." )<br />

Figure 3.27:<br />

puzzle.<br />

An illustration of the Towers of Hanoi<br />

C-3.12<br />

Describe a recursive method for convert<strong>in</strong>g a str<strong>in</strong>g of digits <strong>in</strong>to the <strong>in</strong>teger it<br />

represents. For example, "13531" represents the <strong>in</strong>teger 13,531.<br />

C-3.13<br />

Describe a recursive algorithm that counts the number of nodes <strong>in</strong> a s<strong>in</strong>gly<br />

l<strong>in</strong>ked list.<br />

C-3.14<br />

208

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

Saved successfully!

Ooh no, something went wrong!