Source of List.java


  1: // Fig. 17.3: List.java
  2: // ListNode and List class definitions.
  3: package com.deitel.jhtp6.ch17;
  4: 
  5: // class to represent one node in a list
  6: class ListNode 
  7: {
  8:    // package access members; List can access these directly
  9:    Object data;    
 10:    ListNode nextNode;
 11: 
 12:    // constructor creates a ListNode that refers to object
 13:    ListNode( Object object ) 
 14:    { 
 15:       this( object, null ); 
 16:    } // end ListNode one-argument constructor 
 17: 
 18:    // constructor creates ListNode that refers to 
 19:    // Object and to next ListNode
 20:    ListNode( Object object, ListNode node )
 21:    {
 22:       data = object;    
 23:       nextNode = node;  
 24:    } // end ListNode two-argument constructor
 25: 
 26:    // return reference to data in node
 27:    Object getObject() 
 28:    { 
 29:       return data; // return Object in this node
 30:    } // end method getObject
 31: 
 32:    // return reference to next node in list
 33:    ListNode getNext() 
 34:    { 
 35:       return nextNode; // get next node
 36:    } // end method getNext
 37: } // end class ListNode
 38: 
 39: // class List definition
 40: public class List 
 41: {
 42:    private ListNode firstNode;
 43:    private ListNode lastNode;
 44:    private String name; // string like "list" used in printing
 45: 
 46:    // constructor creates empty List with "list" as the name
 47:    public List() 
 48:    { 
 49:       this( "list" ); 
 50:    } // end List no-argument constructor
 51: 
 52:    // constructor creates an empty List with a name
 53:    public List( String listName )
 54:    {
 55:       name = listName;
 56:       firstNode = lastNode = null;
 57:    } // end List one-argument constructor
 58: 
 59:    // insert Object at front of List
 60:    public void insertAtFront( Object insertItem )
 61:    {
 62:       if ( isEmpty() ) // firstNode and lastNode refer to same object
 63:          firstNode = lastNode = new ListNode( insertItem );
 64:       else // firstNode refers to new node
 65:          firstNode = new ListNode( insertItem, firstNode );
 66:    } // end method insertAtFront
 67: 
 68:    // insert Object at end of List
 69:    public void insertAtBack( Object insertItem )
 70:    {
 71:       if ( isEmpty() ) // firstNode and lastNode refer to same Object
 72:          firstNode = lastNode = new ListNode( insertItem );
 73:       else // lastNode's nextNode refers to new node
 74:          lastNode = lastNode.nextNode = new ListNode( insertItem );
 75:    } // end method insertAtBack
 76: 
 77:    // remove first node from List
 78:    public Object removeFromFront() throws EmptyListException
 79:    {
 80:       if ( isEmpty() ) // throw exception if List is empty
 81:          throw new EmptyListException( name );
 82: 
 83:       Object removedItem = firstNode.data; // retrieve data being removed
 84: 
 85:       // update references firstNode and lastNode 
 86:       if ( firstNode == lastNode )
 87:          firstNode = lastNode = null;
 88:       else
 89:          firstNode = firstNode.nextNode;
 90: 
 91:       return removedItem; // return removed node data
 92:    } // end method removeFromFront
 93: 
 94:    // remove last node from List
 95:    public Object removeFromBack() throws EmptyListException
 96:    {
 97:       if ( isEmpty() ) // throw exception if List is empty
 98:          throw new EmptyListException( name );
 99: 
100:       Object removedItem = lastNode.data; // retrieve data being removed
101: 
102:       // update references firstNode and lastNode
103:       if ( firstNode == lastNode )
104:          firstNode = lastNode = null;
105:       else // locate new last node
106:       { 
107:          ListNode current = firstNode;
108: 
109:          // loop while current node does not refer to lastNode
110:          while ( current.nextNode != lastNode )
111:             current = current.nextNode;
112:    
113:          lastNode = current; // current is new lastNode
114:          current.nextNode = null;
115:       } // end else
116: 
117:       return removedItem; // return removed node data
118:    } // end method removeFromBack
119: 
120:    // determine whether list is empty
121:    public boolean isEmpty()
122:    { 
123:       return firstNode == null; // return true if List is empty
124:    } // end method isEmpty
125: 
126:    // output List contents
127:    public void print()
128:    {
129:       if ( isEmpty() ) 
130:       {
131:          System.out.printf( "Empty %s\n", name );
132:          return;
133:       } // end if
134: 
135:       System.out.printf( "The %s is: ", name );
136:       ListNode current = firstNode;
137: 
138:       // while not at end of list, output current node's data
139:       while ( current != null ) 
140:       {
141:          System.out.printf( "%s ", current.data );
142:          current = current.nextNode;
143:       } // end while
144: 
145:       System.out.println( "\n" );
146:    } // end method print
147: } // end class List
148: 
149: /**************************************************************************
150:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
151:  * Pearson Education, Inc. All Rights Reserved.                           *
152:  *                                                                        *
153:  * DISCLAIMER: The authors and publisher of this book have used their     *
154:  * best efforts in preparing the book. These efforts include the          *
155:  * development, research, and testing of the theories and programs        *
156:  * to determine their effectiveness. The authors and publisher make       *
157:  * no warranty of any kind, expressed or implied, with regard to these    *
158:  * programs or to the documentation contained in these books. The authors *
159:  * and publisher shall not be liable in any event for incidental or       *
160:  * consequential damages in connection with, or arising out of, the       *
161:  * furnishing, performance, or use of these programs.                     *
162:  *************************************************************************/