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