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.

push(e): Insert element e, to be the top of the stack.<br />

pop(): Remove from the stack <strong>and</strong> return the top element on the<br />

stack; an error occurs if the stack is empty.<br />

Additionally, let us also def<strong>in</strong>e the follow<strong>in</strong>g methods:<br />

size(): Return the number of elements <strong>in</strong> the stack.<br />

isEmpty(): Return a Boolean <strong>in</strong>dicat<strong>in</strong>g if the stack is empty.<br />

top(): Return the top element <strong>in</strong> the stack, without remov<strong>in</strong>g it; an<br />

error occurs if the stack is empty.<br />

Example 5.3: The follow<strong>in</strong>g table shows a series of stack operations <strong>and</strong> their<br />

effects on an <strong>in</strong>itially empty stack S of <strong>in</strong>tegers.<br />

Operation<br />

Output<br />

Stack Contents<br />

push(5)<br />

-<br />

(5)<br />

push(3)<br />

-<br />

(5, 3)<br />

pop()<br />

3<br />

(5)<br />

push(7)<br />

-<br />

(5, 7)<br />

pop()<br />

262

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

Saved successfully!

Ooh no, something went wrong!