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.

A Generic NodeStack Class<br />

A <strong>Java</strong> implementation of a stack, by means of a generic s<strong>in</strong>gly l<strong>in</strong>ked list, is<br />

given <strong>in</strong> Code Fragment 5.7. All the methods of the Stack <strong>in</strong>terface are executed<br />

<strong>in</strong> constant time. In addition to be<strong>in</strong>g time efficient, this l<strong>in</strong>ked list<br />

implementation has a space requirement that is O(n), where n is the current<br />

number of elements <strong>in</strong> the stack. Thus, this implementation does not require that a<br />

new exception be created to h<strong>and</strong>le size overflow problems. We use an <strong>in</strong>stance<br />

variable top to refer to the head of the list (which po<strong>in</strong>ts to the null object if the<br />

list is empty). When we push a new element e on the stack, we simply create a<br />

new node v for e, reference e from v, <strong>and</strong> <strong>in</strong>sert v at the head of the list. Likewise,<br />

when we pop an element from the stack, we simply remove the node at the head<br />

276

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

Saved successfully!

Ooh no, something went wrong!