Java Reference
In-Depth Information
Display 15.27
A Stack Class (part 2 of 2)
8 public Node( )
9 {
10 item = null ;
11 link = null ;
12 }
13 public Node(String newItem, Node linkValue)
14 {
15 item = newItem;
16 link = linkValue;
17 }
18 } //End of Node inner class
19
private Node head;
20 public Stack( )
21 {
22 head = null ;
23 }
24
/**
25
This method replaces addToStart
26 */
27 public void push(String itemName)
28 {
29 head = new Node(itemName, head);
30 }
31
/**
32
This method replaces deleteHeadNode and
33
also returns the value popped from the list
34 */
35 public String pop( )
36 {
37
if (head == null )
38
throw new IllegalStateException( );
39 else
40 {
41 String returnItem = head.item;
42 head = head.link;
43
return returnItem;
44 }
45 }
46 public boolean isEmpty( )
47 {
48
return (head == null );
49 }
50 }
 
Search WWH ::




Custom Search