1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 package HTTPClient;
34
35
36
37
38
39
40
41
42 class LinkedList
43 {
44
45 private LinkElement head = null;
46
47
48 private LinkElement tail = null;
49
50
51
52
53
54
55
56 public synchronized void addToHead(Object elem)
57 {
58 head = new LinkElement(elem, head);
59
60 if (head.next == null)
61 tail = head;
62 }
63
64
65
66
67
68
69
70 public synchronized void addToEnd(Object elem)
71 {
72 if (head == null)
73 head = tail = new LinkElement(elem, null);
74 else
75 tail = (tail.next = new LinkElement(elem, null));
76 }
77
78
79
80
81
82
83
84
85 public synchronized void remove(Object elem)
86 {
87 if (head == null) return;
88
89 if (head.element == elem)
90 {
91 head = head.next;
92 return;
93 }
94
95 LinkElement curr = head;
96 while (curr.next != null)
97 {
98 if (curr.next.element == elem)
99 {
100 if (curr.next == tail) tail = curr;
101 curr.next = curr.next.next;
102 return;
103 }
104 curr = curr.next;
105 }
106 }
107
108
109
110
111
112
113
114
115 public synchronized Object getFirst()
116 {
117 if (head == null) return null;
118 return head.element;
119 }
120
121
122 private LinkElement next_enum = null;
123
124
125
126
127
128
129
130 public synchronized Object enumerate()
131 {
132 if (head == null) return null;
133
134 next_enum = head.next;
135 return head.element;
136 }
137
138
139
140
141
142
143
144
145
146 public synchronized Object next()
147 {
148 if (next_enum == null) return null;
149
150 Object elem = next_enum.element;
151 next_enum = next_enum.next;
152
153 return elem;
154 }
155
156
157 public static void main(String args[]) throws Exception
158 {
159
160
161 System.err.println("\n*** Linked List Tests ...");
162
163 LinkedList list = new LinkedList();
164 list.addToHead("One");
165 list.addToEnd("Last");
166 if (!list.getFirst().equals("One"))
167 throw new Exception("First element wrong");
168 if (!list.enumerate().equals("One"))
169 throw new Exception("First element wrong");
170 if (!list.next().equals("Last"))
171 throw new Exception("Last element wrong");
172 if (list.next() != null)
173 throw new Exception("End of list wrong");
174 list.remove("One");
175 if (!list.getFirst().equals("Last"))
176 throw new Exception("First element wrong");
177 list.remove("Last");
178 if (list.getFirst() != null)
179 throw new Exception("End of list wrong");
180
181 list = new LinkedList();
182 list.addToEnd("Last");
183 list.addToHead("One");
184 if (!list.getFirst().equals("One"))
185 throw new Exception("First element wrong");
186 if (!list.enumerate().equals("One"))
187 throw new Exception("First element wrong");
188 if (!list.next().equals("Last"))
189 throw new Exception("Last element wrong");
190 if (list.next() != null)
191 throw new Exception("End of list wrong");
192 if (!list.enumerate().equals("One"))
193 throw new Exception("First element wrong");
194 list.remove("One");
195 if (!list.next().equals("Last"))
196 throw new Exception("Last element wrong");
197 list.remove("Last");
198 if (list.next() != null)
199 throw new Exception("End of list wrong");
200
201 list = new LinkedList();
202 list.addToEnd("Last");
203 list.addToHead("Two");
204 list.addToHead("One");
205 if (!list.getFirst().equals("One"))
206 throw new Exception("First element wrong");
207 if (!list.enumerate().equals("One"))
208 throw new Exception("First element wrong");
209 if (!list.next().equals("Two"))
210 throw new Exception("Second element wrong");
211 if (!list.next().equals("Last"))
212 throw new Exception("Last element wrong");
213 if (list.next() != null)
214 throw new Exception("End of list wrong");
215 list.remove("Last");
216 list.remove("Two");
217 list.remove("One");
218 if (list.getFirst() != null)
219 throw new Exception("Empty list wrong");
220
221 System.err.println("\n*** Tests finished successfuly");
222 }
223 }
224
225
226
227
228
229 class LinkElement
230 {
231 Object element;
232 LinkElement next;
233
234 LinkElement(Object elem, LinkElement next)
235 {
236 this.element = elem;
237 this.next = next;
238 }
239 }