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