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