Source of StringLinkedListWithIterator.java


  1: 
  2: /**
  3:  Linked list with an iterator. One node is the "current node." 
  4:  Initially, the current node is the first node. It can be changed
  5:  to the next node, until the iteration has moved beyond the end 
  6:  of the list.
  7: */
  8: public class StringLinkedListWithIterator
  9: {
 10:     private ListNode head;
 11:     private ListNode current;
 12:     private ListNode previous;
 13: 
 14:     public StringLinkedListWithIterator( )
 15:     {
 16:         head = null;
 17:         current = null;
 18:         previous = null;
 19:     }
 20: 
 21:     public void addANodeToStart(String addData)
 22:     {
 23:         head = new ListNode(addData, head);
 24:         if ((current == head.link) && (current != null))
 25:         //if current is at old start node
 26:             previous = head;
 27:     }
 28: 
 29:         /**
 30:          Sets iterator to beginning of list.
 31:         */
 32:         public void resetIteration( )
 33:     {
 34:         current = head;
 35:         previous = null;
 36:     }
 37: 
 38:     /**
 39:      Returns true if iteration is not finished.
 40:     */
 41:      public boolean moreToIterate( )
 42:      {
 43:          return current != null;
 44:      }
 45: 
 46:     /**
 47:      Advances iterator to next node.
 48:     */
 49:      public void goToNext( )
 50:      {
 51:          if (current != null)
 52:          {
 53:              previous = current;
 54:              current = current.link;
 55:          }
 56:          else if (head != null)
 57:          {
 58:              System.out.println(
 59:                 "Iterated too many times or uninitialized iteration.");
 60:              System.exit(0);
 61:          }
 62:          else
 63:          {
 64:              System.out.println("Iterating with an empty list.");
 65:              System.exit(0);
 66:          }
 67:      }
 68: 
 69:     /**
 70:      Returns the data at the current node.
 71:     */
 72:      public String getDataAtCurrent( )
 73:      {
 74:         String result = null;
 75:         if (current != null)
 76:             result = current.data;
 77:         else
 78:         {
 79:             System.out.println(
 80:                     "Getting data when current is not at any node.");
 81:             System.exit(0);
 82:         }
 83:         return result;
 84:         }
 85: 
 86:     /**
 87:      Replaces the data at the current node.
 88:     */
 89:         public void setDataAtCurrent(String newData)
 90:     {
 91:         if (current != null)
 92:         {
 93:             current.data = newData;
 94:         }
 95:         else
 96:         {
 97:             System.out.println(
 98:                  "Setting data when current is not at any node.");
 99:             System.exit(0);
100:         }
101:     }
102: 
103:     /**
104:      Inserts a new node containing newData after the current node.
105:      The current node is the same after invocation as it is before.
106:      Precondition: List is not empty; current node is not
107:      beyond the entire list.
108:         */
109:     public void insertNodeAfterCurrent(String newData)
110:     {
111:         ListNode newNode = new ListNode( );
112:         newNode.data = newData;
113:         if (current != null)
114:         {
115:             newNode.link = current.link;
116:             current.link = newNode;
117:         }
118:         else if (head != null)
119:         {
120:             System.out.println(
121:                         "Inserting when iterator is past all " +
122:                         "nodes or is not initialized.");
123:             System.exit(0);
124:         }
125:         else
126:         {
127:             System.out.println(
128:                   "Using insertNodeAfterCurrent with empty list.");
129:             System.exit(0);
130:         }
131:     }
132: 
133:     /**
134:      Deletes the current node. After the invocation, 
135:      the current node is either the node after the 
136:      deleted node or null if there is no next node.
137:         */
138:     public void deleteCurrentNode( )
139:     {
140:         if ((current != null) && (previous != null))
141:         {
142:             previous.link = current.link;
143:             current = current.link;
144:         }
145:         else if ((current != null) && (previous == null))
146:         {   //At head node
147:             head = current.link;
148:             current = head;
149:         }
150:         else //current == null
151:         {
152:             System.out.println(
153:              "Deleting with uninitialized current or an empty list.");
154:             System.exit(0);
155:         }
156:     }
157: 
158:     public void showList( )
159:     {
160:         ListNode position = head;
161:         while (position != null)
162:         {
163:             System.out.println(position.data);
164:             position = position.link;
165:         }
166:     }
167: 
168:     public int length( )
169:     {
170:         int count = 0;
171:         ListNode position = head;
172:         while (position != null)
173:         {
174:             count++;
175:             position = position.link;
176:         }
177:         return count;
178:     }
179: 
180:     public boolean onList(String target)
181:     {
182:         return find(target) != null;
183:     }
184: 
185:         private ListNode find(String target)
186:     {
187:                 boolean found = false;
188:         ListNode position = head;
189:         while ((position != null) && !found)
190:         {
191:             String dataAtPosition = position.data;
192:             if (dataAtPosition.equals(target))
193:                                 found = true;
194:                         else
195:                                 position = position.link;
196:         }
197: 
198:         return position;
199:     }
200:         
201:     public String[] toArray( )
202:     {
203:         String[] a = new String[length( )];
204: 
205:         ListNode position = head;
206:         int i = 0;
207:         while (position != null)
208:         {
209:             a[i] = position.data;
210:             i++;
211:             position = position.link;
212:         }
213: 
214:         return a;
215:     }
216: 
217:     private class ListNode
218:     {
219:         private String data;
220:         private ListNode link;
221:                 
222:         public ListNode( )
223:         {
224:             link = null;
225:             data = null;
226:         }
227:                 
228:         public ListNode(String newData, ListNode linkValue)
229:         {
230:             data = newData;
231:             link = linkValue;
232:         }
233:     }
234: }