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

Alice has three array-based stacks, A, B, <strong>and</strong> C, such that A has capacity 100, B<br />

has capacity 5, <strong>and</strong> C has capacity 3. Initially, A is full, <strong>and</strong> B <strong>and</strong> C are empty.<br />

Unfortunately, the person who programmed the class for these stacks made the<br />

push <strong>and</strong> pop methods private. The only method Alice can use is a static<br />

method, transfer(S,T), which transfers (by itera-tively apply<strong>in</strong>g the private pop<br />

<strong>and</strong> push methods) elements from stack S to stack T until either S becomes<br />

empty or T becomes full. So, for example, start<strong>in</strong>g from our <strong>in</strong>itial configuration<br />

<strong>and</strong> perform<strong>in</strong>g transfer(A, C) results <strong>in</strong> A now hold<strong>in</strong>g 97 elements <strong>and</strong> C<br />

hold<strong>in</strong>g 3. Describe a sequence of transfer operations that starts from the<br />

<strong>in</strong>itial configuration <strong>and</strong> results <strong>in</strong> B hold<strong>in</strong>g 4 elements at the end.<br />

C-5.11<br />

Alice has two queues, S <strong>and</strong> T, which can store <strong>in</strong>tegers. Bob gives Alice 50 odd<br />

<strong>in</strong>tegers <strong>and</strong> 50 even <strong>in</strong>tegers <strong>and</strong> <strong>in</strong>sists that she stores all 100 <strong>in</strong>tegers <strong>in</strong> S <strong>and</strong><br />

T. They then play a game where Bob picks S or T at r<strong>and</strong>om <strong>and</strong> then applies<br />

the round-rob<strong>in</strong> scheduler, described <strong>in</strong> the chapter, to the chosen queue a<br />

r<strong>and</strong>om number of times. If the number left out of the queue at the end of this<br />

game is odd, Bob w<strong>in</strong>s. Otherwise, Alice w<strong>in</strong>s. How can Alice allocate <strong>in</strong>tegers<br />

to queues to optimize her chances of w<strong>in</strong>n<strong>in</strong>g? What is her chance of w<strong>in</strong>n<strong>in</strong>g?<br />

C-5.12<br />

Suppose Bob has four cows that he wants to take across a bridge, but only one<br />

yoke, which can hold up to two cows, side by side, tied to the yoke. The yoke is<br />

too heavy for him to carry across the bridge, but he can tie (<strong>and</strong> untie) cows to it<br />

<strong>in</strong> no time at all. Of his four cows, Mazie can cross the bridge <strong>in</strong> 2 m<strong>in</strong>utes,<br />

Daisy can cross it <strong>in</strong> 4 m<strong>in</strong>utes, Crazy can cross it <strong>in</strong> 10 m<strong>in</strong>utes, <strong>and</strong> Lazy can<br />

cross it <strong>in</strong> 20 m<strong>in</strong>utes. Of course, when two cows are tied to the yoke, they must<br />

go at the speed of the slower cow. Describe how Bob can get all his cows across<br />

the bridge <strong>in</strong> 34 m<strong>in</strong>utes.<br />

Projects<br />

P-5.1<br />

Implement the stack ADT with a doubly l<strong>in</strong>ked list.<br />

P-5.2<br />

Implement the stack ADT us<strong>in</strong>g the <strong>Java</strong> ArrayList class (without us<strong>in</strong>g the<br />

built-<strong>in</strong> <strong>Java</strong> Stack class).<br />

P-5.3<br />

308

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

Saved successfully!

Ooh no, something went wrong!