View Javadoc

1   /*
2    * @(#)LinkedList.java					0.3-3 06/05/2001
3    *
4    *  This file is part of the HTTPClient package
5    *  Copyright (C) 1996-2001 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   *  The HTTPClient's home page is located at:
28   *
29   *  http://www.innovation.ch/java/HTTPClient/ 
30   *
31   */
32  
33  package HTTPClient;
34  
35  
36  /**
37   * This class implements a singly linked list.
38   *
39   * @version	0.3-3  06/05/2001
40   * @author	Ronald Tschalär
41   */
42  class LinkedList
43  {
44      /** head of list */
45      private LinkElement head = null;
46  
47      /** tail of list (for faster adding) */
48      private LinkElement tail = null;
49  
50  
51      /**
52       * Add the specified element to the head of the list.
53       *
54       * @param elem the object to add to the list.
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       * Add the specified element to the end of the list.
67       *
68       * @param elem the object to add to the list.
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       * Remove the specified element from the list. Does nothing if the element
81       * is not in the list.
82       *
83       * @param elem the object to remove from the list.
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      * Return the first element in the list. The list is not modified in any
111      * way.
112      *
113      * @return the first element
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      * Starts an enumeration of all the elements in this list. Note that only
126      * one enumeration can be active at any time.
127      *
128      * @return the first element, or null if the list is empty
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      * Gets the next element in the enumeration. The enumeration must have
141      * been first initalized with a call to <code>enumerate()</code>.
142      *
143      * @return the next element, or null if none left
144      * @see #enumerate()
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 	// LinkedList Test Suite
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  * The represents a single element in the linked list.
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 }