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.

Analyz<strong>in</strong>g the Array-Based Stack Implementation<br />

The correctness of the methods <strong>in</strong> the array-based implementation follows<br />

immediately from the def<strong>in</strong>ition of the methods themselves. There is,<br />

nevertheless, a mildly <strong>in</strong>terest<strong>in</strong>g po<strong>in</strong>t here <strong>in</strong>volv<strong>in</strong>g the implementation of the<br />

pop method.<br />

Note that we could have avoided resett<strong>in</strong>g the old S[t] to null <strong>and</strong> we would still<br />

have a correct method. There is a trade-off <strong>in</strong> be<strong>in</strong>g able to avoid this assignment<br />

should we be th<strong>in</strong>k<strong>in</strong>g about implement<strong>in</strong>g these algorithms <strong>in</strong> <strong>Java</strong>, however.<br />

The trade-off <strong>in</strong>volves the <strong>Java</strong> garbage collection mechanism that searches<br />

memory for objects that are no longer referenced by active objects, <strong>and</strong> reclaims<br />

their space for future use. (For more details, see Section 14.1.3.) Let e = S[t] be<br />

the top element before the pop method is called. By mak<strong>in</strong>g S[t] a null reference,<br />

268

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

Saved successfully!

Ooh no, something went wrong!