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.

(9, 7, 3, 5)<br />

size()<br />

4<br />

(9, 7, 3, 5)<br />

pop()<br />

5<br />

(9, 7, 3)<br />

push(8)<br />

-<br />

(9, 7, 3, 8)<br />

pop()<br />

8<br />

(9, 7, 3)<br />

pop()<br />

3<br />

(9, 7)<br />

A Stack Interface <strong>in</strong> <strong>Java</strong><br />

Because of its importance, the stack data structure is <strong>in</strong>cluded as a "built-<strong>in</strong>" class<br />

<strong>in</strong> the java.util package of <strong>Java</strong>. Class java.util.Stack is a data<br />

structure that stores generic <strong>Java</strong> objects <strong>and</strong> <strong>in</strong>cludes, among others, the methods<br />

push(), pop(), peek() (equivalent to top()), size(), <strong>and</strong> empty()<br />

(equivalent to isEmpty()). Methods pop() <strong>and</strong> peek() throw exception<br />

EmptyStackException if they are called on an empty stack. While it is<br />

convenient to just use the built-<strong>in</strong> class java.util.Stack, it is <strong>in</strong>structive to<br />

learn how to design <strong>and</strong> implement a stack "from scratch."<br />

Implement<strong>in</strong>g an abstract data type <strong>in</strong> <strong>Java</strong> <strong>in</strong>volves two steps. The first step is the<br />

def<strong>in</strong>ition of a <strong>Java</strong> Application Programm<strong>in</strong>g Interface (API), or simply<br />

<strong>in</strong>terface, which describes the names of the methods that the ADT supports <strong>and</strong><br />

how they are to be declared <strong>and</strong> used.<br />

264

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

Saved successfully!

Ooh no, something went wrong!