Java Reference
In-Depth Information
1 package weiss.nonstandard;
2
3 // ListStack class
4 //
5 // CONSTRUCTION: with no initializer
6 //
7 // ******************PUBLIC OPERATIONS*********************
8 // void push( x ) --> Insert x
9 // void pop( ) --> Remove most recently inserted item
10 // AnyType top( ) --> Return most recently inserted item
11 // AnyType topAndPop( ) --> Return and remove most recent item
12 // boolean isEmpty( ) --> Return true if empty; else false
13 // void makeEmpty( ) --> Remove all items
14 // ******************ERRORS********************************
15 // top, pop, or topAndPop on empty stack
16
17 public class ListStack<AnyType> implements Stack<AnyType>
18 {
19 public boolean isEmpty( )
20 { return topOfStack == null; }
21 public void makeEmpty( )
22 { topOfStack = null; }
23
24 public void push( AnyType x )
25 { /* Figure 16.20 */ }
26 public void pop( )
27 { /* Figure 16.20 */ }
28 public AnyType top( )
29 { /* Figure 16.21 */ }
30 public AnyType topAndPop( )
31 { /* Figure 16.21 */ }
32
33 private ListNode<AnyType> topOfStack = null;
34 }
35
36 // Basic node stored in a linked list.
37 // Note that this class is not accessible outside
38 // of package weiss.nonstandard
39 class ListNode<AnyType>
40 {
41 public ListNode( AnyType theElement )
42 { this( theElement, null ); }
43
44 public ListNode( AnyType theElement, ListNode<AnyType> n )
45 { element = theElement; next = n; }
46
47 public AnyType element;
48 public ListNode next;
49 }
figure 16.19
Skeleton for
linked list-based
stack class
Search WWH ::




Custom Search