27.10.2014 Views

Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions

Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions

Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions

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.

<strong>Solutions</strong> to Chapter 3 | Stacks <strong>and</strong> Queues<br />

13 disks = new Stack();<br />

14 index = i;<br />

15 }<br />

16<br />

17 public int index() {<br />

18 return index;<br />

19 }<br />

20<br />

21 public void add(int d) {<br />

22 if (!disks.isEmpty() && disks.peek() = 0; i--) {<br />

39 System.out.println(“ “ + disks.get(i));<br />

40 }<br />

41 }<br />

42<br />

43 public void moveDisks(int n, Tower destination, Tower buffer) {<br />

44 if (n > 0) {<br />

45 moveDisks(n - 1, buffer, destination);<br />

46 moveTopTo(destination);<br />

47 buffer.moveDisks(n - 1, destination, this);<br />

48 }<br />

49 }<br />

50 }<br />

1 1 9<br />

<strong>Cracking</strong> <strong>the</strong> <strong>Coding</strong> <strong>Interview</strong> | Data Structures

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

Saved successfully!

Ooh no, something went wrong!