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.

of the list <strong>and</strong> return its element. Thus, we perform all <strong>in</strong>sertions <strong>and</strong> removals of<br />

elements at the head of the list.<br />

Code Fragment 5.7: Class NodeStack, which<br />

implements the Stack <strong>in</strong>terface us<strong>in</strong>g a s<strong>in</strong>gly l<strong>in</strong>ked<br />

list, whose nodes are objects of class Node from Code<br />

Fragment 5.6.<br />

5.1.4 Revers<strong>in</strong>g an Array Us<strong>in</strong>g a Stack<br />

We can use a stack to reverse the elements <strong>in</strong> an array, thereby produc<strong>in</strong>g a<br />

nonrecursive algorithm for the array-reversal problem <strong>in</strong>troduced <strong>in</strong> Section 3.5.1.<br />

The basic idea is simply to push all the elements of the array <strong>in</strong> order <strong>in</strong>to a stack<br />

277

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

Saved successfully!

Ooh no, something went wrong!