Source of StringLinkedListSelf.java


  1: 
  2: //public class StringLinkedListSelfContained
  3: public class StringLinkedListSelf
  4: {
  5:     private ListNode head;
  6: 
  7: //    public StringLinkedListSelfContained( )
  8:     public StringLinkedListSelf( )
  9:     {
 10:         head = null;
 11:     }
 12: 
 13:     /**
 14:      Displays the data on the list.
 15:     */
 16:     public void showList( )
 17:     {
 18:         ListNode position = head;
 19:         while (position != null)
 20:         {
 21:             System.out.println(position.data);
 22:             position = position.link;
 23:         }
 24:     }
 25: 
 26:     /**
 27:      Returns the number of nodes on the list.
 28:     */
 29:     public int length( )
 30:     {
 31:         int count = 0;
 32:         ListNode position = head;
 33:         while (position != null)
 34:         {
 35:             count++;
 36:             position = position.link;
 37:         }
 38:         return count;
 39:     }
 40: 
 41:     /**
 42:      Adds a node containing the data addData at the 
 43:      start of the list.
 44:         */
 45:     public void addANodeToStart(String addData)
 46:     {
 47:         head = new ListNode(addData, head);
 48:     }
 49: 
 50:     /**
 51:      Deletes the first node on the list.
 52:     */
 53:     public void deleteHeadNode( )
 54:     {
 55:         if (head != null)
 56:             head = head.link;
 57:         else
 58:         {
 59:             System.out.println("Deleting from an empty list.");
 60:             System.exit(0);
 61:         }
 62:     }
 63: 
 64:     /**
 65:      Sees whether target is on the list.
 66:     */
 67:     public boolean onList(String target)
 68:     {
 69:         return find(target) != null;
 70:     }
 71: 
 72:     // Returns a reference to the first node containing the
 73:     // target data. If target is not on the list, returns null.
 74:         private ListNode find(String target)
 75:     {
 76:                 boolean found = false;
 77:         ListNode position = head;
 78:         while ((position != null) && !found)
 79:         {
 80:             String dataAtPosition = position.data;
 81:             if (dataAtPosition.equals(target))
 82:                                 found = true;
 83:                         else
 84:                                 position = position.link;
 85:         }
 86: 
 87:         return position;
 88:     }
 89: 
 90:     /**
 91:      Returns an array of the elements on the list.
 92:     */
 93:     public String[] toArray( )
 94:     {
 95:         String[] anArray = new String[length( )];
 96: 
 97:         ListNode position = head;
 98:         int i = 0;
 99:         while (position != null)
100:         {
101:             anArray[i] = position.data;
102:             i++;
103:             position = position.link;
104:         }
105: 
106:         return anArray;
107:     }
108: 
109:     private class ListNode
110:     {
111:         private String   data;
112:         private ListNode link;
113:                 
114:         public ListNode( )
115:         {
116:             link = null;
117:             data = null;
118:         }
119:                 
120:         public ListNode(String newData, ListNode linkValue)
121:         {
122:             data = newData;
123:             link = linkValue;
124:         }
125:     }
126: }