26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 19 Data Structures 1109<br />

occurs, the successive return addresses are pushed on<strong>to</strong> the stack in last-in, first-out order<br />

so that each method can return <strong>to</strong> its caller. Stacks support recursive method calls in the<br />

same manner as they do conventional nonrecursive method calls.<br />

The program execution stack contains the memory for local variables on each invocation<br />

of a method during a program’s execution. When the method returns <strong>to</strong> its caller, the<br />

memory for that method’s local variables is popped off the stack and those variables are no<br />

longer known <strong>to</strong> the program.<br />

Compilers use stacks <strong>to</strong> evaluate arithmetic expressions and generate machine language<br />

code <strong>to</strong> process the expressions. The exercises in this chapter explore several applications<br />

of stacks, including using them <strong>to</strong> develop a complete working compiler. The<br />

java.util package of the <strong>Java</strong> API contains class Stack for implementing and manipulating<br />

stacks that can grow and shrink during program execution. We will discuss class<br />

Stack in Chapter 20.<br />

We take advantage of the close relationship between lists and stacks <strong>to</strong> implement a<br />

stack class by reusing a list class. We demonstrate two different forms of reusability. First,<br />

we implement the stack class by extending class List of Fig. 19.3. Then we implement an<br />

identically performing stack class through composition by including a List object as a<br />

private member of a stack class. The list, stack and queue data structures in this chapter<br />

are implemented <strong>to</strong> s<strong>to</strong>re Object references <strong>to</strong> encourage further reusability. Thus, any<br />

object type can be s<strong>to</strong>red in a list, stack or queue.<br />

The application of Fig. 19.10 and Fig. 19.11 creates a stack class by extending class<br />

List of Fig. 19.3. We want the stack <strong>to</strong> have methods push, pop, isEmpty and print.<br />

Essentially, these are the methods insertAtFront, removeFromFront, isEmpty<br />

and print of class List. Of course, class List contains other methods (such as<br />

insertAtBack and removeFromBack) that we would rather not make accessible<br />

through the public interface <strong>to</strong> the stack class. It is important <strong>to</strong> remember that all<br />

methods in the public interface of class List class also are public methods of the subclass<br />

StackInheritance (Fig. 19.10). When we implement the stack’s methods, we<br />

have each StackInheritance method call the appropriate List method—method<br />

push calls insertAtFront and method pop calls removeFromFront. Class<br />

StackInheritance is defined as part of package com.deitel.jhtp4.ch19 for<br />

reuse purposes. Note that StackInheritance does not import List, because both<br />

classes are in the same package.<br />

1 // Fig. 19.10: StackInheritance.java<br />

2 // Derived from class List<br />

3 package com.deitel.jhtp4.ch19;<br />

4<br />

5 public class StackInheritance extends List {<br />

6<br />

7 // construct stack<br />

8 public StackInheritance()<br />

9 {<br />

10 super( "stack" );<br />

11 }<br />

12<br />

Fig. Fig. 19.10 19.10 Class StackInheritance extends class List (part 1 of 2).

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

Saved successfully!

Ooh no, something went wrong!