KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > HTTPClient > LinkedList


1 /*
2  * @(#)LinkedList.java 0.3-2 18/06/1999
3  *
4  * This file is part of the HTTPClient package
5  * Copyright (C) 1996-1999 Ronald Tschalär
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free
19  * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20  * MA 02111-1307, USA
21  *
22  * For questions, suggestions, bug-reports, enhancement-requests etc.
23  * I may be contacted at:
24  *
25  * ronald@innovation.ch
26  *
27  */

28
29 package HTTPClient;
30
31
32 /**
33  * This class implements a singly linked list.
34  *
35  * @version 0.3-2 18/06/1999
36  * @author Ronald Tschalär
37  */

38
39 class LinkedList
40 {
41     /** head of list */
42     private LinkElement head = null;
43
44     /** tail of list (for faster adding) */
45     private LinkElement tail = null;
46
47
48     /**
49      * Add the specified element to the head of the list.
50      *
51      * @param elem the object to add to the list.
52      */

53     public synchronized void addToHead(Object JavaDoc elem)
54     {
55     head = new LinkElement(elem, head);
56
57     if (head.next == null)
58         tail = head;
59     }
60
61
62     /**
63      * Add the specified element to the end of the list.
64      *
65      * @param elem the object to add to the list.
66      */

67     public synchronized void addToEnd(Object JavaDoc elem)
68     {
69     if (head == null)
70         head = tail = new LinkElement(elem, null);
71     else
72         tail = (tail.next = new LinkElement(elem, null));
73     }
74
75
76     /**
77      * Remove the specified element from the list. Does nothing if the element
78      * is not in the list.
79      *
80      * @param elem the object to remove from the list.
81      */

82     public synchronized void remove(Object JavaDoc elem)
83     {
84     if (head == null) return;
85
86     if (head.element == elem)
87     {
88         head = head.next;
89         return;
90     }
91
92     LinkElement curr = head;
93     while (curr.next != null)
94     {
95         if (curr.next.element == elem)
96         {
97         if (curr.next == tail) tail = curr;
98         curr.next = curr.next.next;
99         return;
100         }
101         curr = curr.next;
102     }
103     }
104
105
106     /**
107      * Return the first element in the list. The list is not modified in any
108      * way.
109      *
110      * @return the first element
111      */

112     public synchronized Object JavaDoc getFirst()
113     {
114     if (head == null) return null;
115     return head.element;
116     }
117
118
119     private LinkElement next_enum = null;
120
121     /**
122      * Starts an enumeration of all the elements in this list. Note that only
123      * one enumeration can be active at any time.
124      *
125      * @return the first element, or null if the list is empty
126      */

127     public synchronized Object JavaDoc enumerate()
128     {
129     if (head == null) return null;
130
131     next_enum = head.next;
132     return head.element;
133     }
134
135
136     /**
137      * Gets the next element in the enumeration. The enumeration must have
138      * been first initalized with a call to <code>enumerate()</code>.
139      *
140      * @return the next element, or null if none left
141      * @see #enumerate()
142      */

143     public synchronized Object JavaDoc next()
144     {
145     if (next_enum == null) return null;
146
147     Object JavaDoc elem = next_enum.element;
148     next_enum = next_enum.next;
149
150     return elem;
151     }
152
153
154     public static void main(String JavaDoc args[]) throws Exception JavaDoc
155     {
156     // LinkedList Test Suite
157

158     System.err.println("\n*** Linked List Tests ...");
159
160     LinkedList list = new LinkedList();
161     list.addToHead("One");
162     list.addToEnd("Last");
163     if (!list.getFirst().equals("One"))
164         throw new Exception JavaDoc("First element wrong");
165     if (!list.enumerate().equals("One"))
166         throw new Exception JavaDoc("First element wrong");
167     if (!list.next().equals("Last"))
168         throw new Exception JavaDoc("Last element wrong");
169     if (list.next() != null)
170         throw new Exception JavaDoc("End of list wrong");
171     list.remove("One");
172     if (!list.getFirst().equals("Last"))
173         throw new Exception JavaDoc("First element wrong");
174     list.remove("Last");
175     if (list.getFirst() != null)
176         throw new Exception JavaDoc("End of list wrong");
177
178     list = new LinkedList();
179     list.addToEnd("Last");
180     list.addToHead("One");
181     if (!list.getFirst().equals("One"))
182         throw new Exception JavaDoc("First element wrong");
183     if (!list.enumerate().equals("One"))
184         throw new Exception JavaDoc("First element wrong");
185     if (!list.next().equals("Last"))
186         throw new Exception JavaDoc("Last element wrong");
187     if (list.next() != null)
188         throw new Exception JavaDoc("End of list wrong");
189     if (!list.enumerate().equals("One"))
190         throw new Exception JavaDoc("First element wrong");
191     list.remove("One");
192     if (!list.next().equals("Last"))
193         throw new Exception JavaDoc("Last element wrong");
194     list.remove("Last");
195     if (list.next() != null)
196         throw new Exception JavaDoc("End of list wrong");
197
198     list = new LinkedList();
199     list.addToEnd("Last");
200     list.addToHead("Two");
201     list.addToHead("One");
202     if (!list.getFirst().equals("One"))
203         throw new Exception JavaDoc("First element wrong");
204     if (!list.enumerate().equals("One"))
205         throw new Exception JavaDoc("First element wrong");
206     if (!list.next().equals("Two"))
207         throw new Exception JavaDoc("Second element wrong");
208     if (!list.next().equals("Last"))
209         throw new Exception JavaDoc("Last element wrong");
210     if (list.next() != null)
211         throw new Exception JavaDoc("End of list wrong");
212     list.remove("Last");
213     list.remove("Two");
214     list.remove("One");
215     if (list.getFirst() != null)
216         throw new Exception JavaDoc("Empty list wrong");
217
218     System.err.println("\n*** Tests finished successfuly");
219     }
220 }
221
222
223 /**
224  * The represents a single element in the linked list.
225  */

226 class LinkElement
227 {
228     Object JavaDoc element;
229     LinkElement next;
230
231     LinkElement(Object JavaDoc elem, LinkElement next)
232     {
233     this.element = elem;
234     this.next = next;
235     }
236 }
237
238
Popular Tags