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.

Example Output<br />

Below, we show the output from the above ArrayStack program. Note that,<br />

through the use of generic types, we are able to create an ArrayStack A for<br />

stor<strong>in</strong>g <strong>in</strong>tegers <strong>and</strong> another ArrayStack B that stores character str<strong>in</strong>gs.<br />

------> new ArrayStack A, returns null<br />

result: size = 0, isEmpty = true, stack: []<br />

------> A.push(7), returns null<br />

result: size = 1, isEmpty = false, stack: [7]<br />

------> A.pop(), returns 7<br />

result: size = 0, isEmpty = true, stack: []<br />

------> A.push(9), returns null<br />

result: size = 1, isEmpty = false, stack: [9]<br />

------> A.pop(), returns 9<br />

result: size = 0, isEmpty = true, stack: []<br />

------> new ArrayStack B, returns null<br />

result: size = 0, isEmpty = true, stack: []<br />

------> B.push("Bob"), returns null<br />

result: size = 1, isEmpty = false, stack: [Bob]<br />

------> B.push("Alice"), returns null<br />

result: size = 2, isEmpty = false, stack: [Bob, Alice]<br />

------> B.pop(), returns Alice<br />

result: size = 1, isEmpty = false, stack: [Bob]<br />

------> B.push("Eve"), returns null<br />

result: size = 2, isEmpty = false, stack: [Bob, Eve]<br />

A Drawback with the Array-Based Stack Implementation<br />

274

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

Saved successfully!

Ooh no, something went wrong!