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.

potato is equivalent to dequeu<strong>in</strong>g an element <strong>and</strong> immediately enqueu<strong>in</strong>g it aga<strong>in</strong>.<br />

After this process has been performed k times, we remove the front element by<br />

dequeu<strong>in</strong>g it from the queue <strong>and</strong> discard<strong>in</strong>g it. We show a complete <strong>Java</strong> program<br />

for solv<strong>in</strong>g the Josephus problem us<strong>in</strong>g this approach <strong>in</strong> Code Fragment 5.16,<br />

which describes a solution that runs <strong>in</strong> O(nk) time. (We can solve this problem<br />

faster us<strong>in</strong>g techniques beyond the scope of this book.)<br />

Code Fragment 5.16: A complete <strong>Java</strong> program for<br />

solv<strong>in</strong>g the Josephus problem us<strong>in</strong>g a queue. Class<br />

NodeQueue is shown <strong>in</strong> Code Fragment 5.15.<br />

297

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

Saved successfully!

Ooh no, something went wrong!