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