Source of StringLinkedListWithIterator.java


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