001: /*
002: * @(#)LinkedList.java 0.3-2 18/06/1999
003: *
004: * This file is part of the HTTPClient package
005: * Copyright (C) 1996-1999 Ronald Tschalär
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free
019: * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
020: * MA 02111-1307, USA
021: *
022: * For questions, suggestions, bug-reports, enhancement-requests etc.
023: * I may be contacted at:
024: *
025: * ronald@innovation.ch
026: *
027: */
028:
029: package HTTPClient;
030:
031: /**
032: * This class implements a singly linked list.
033: *
034: * @version 0.3-2 18/06/1999
035: * @author Ronald Tschalär
036: */
037:
038: class LinkedList {
039: /** head of list */
040: private LinkElement head = null;
041:
042: /** tail of list (for faster adding) */
043: private LinkElement tail = null;
044:
045: /**
046: * Add the specified element to the head of the list.
047: *
048: * @param elem the object to add to the list.
049: */
050: public synchronized void addToHead(Object elem) {
051: head = new LinkElement(elem, head);
052:
053: if (head.next == null)
054: tail = head;
055: }
056:
057: /**
058: * Add the specified element to the end of the list.
059: *
060: * @param elem the object to add to the list.
061: */
062: public synchronized void addToEnd(Object elem) {
063: if (head == null)
064: head = tail = new LinkElement(elem, null);
065: else
066: tail = (tail.next = new LinkElement(elem, null));
067: }
068:
069: /**
070: * Remove the specified element from the list. Does nothing if the element
071: * is not in the list.
072: *
073: * @param elem the object to remove from the list.
074: */
075: public synchronized void remove(Object elem) {
076: if (head == null)
077: return;
078:
079: if (head.element == elem) {
080: head = head.next;
081: return;
082: }
083:
084: LinkElement curr = head;
085: while (curr.next != null) {
086: if (curr.next.element == elem) {
087: if (curr.next == tail)
088: tail = curr;
089: curr.next = curr.next.next;
090: return;
091: }
092: curr = curr.next;
093: }
094: }
095:
096: /**
097: * Return the first element in the list. The list is not modified in any
098: * way.
099: *
100: * @return the first element
101: */
102: public synchronized Object getFirst() {
103: if (head == null)
104: return null;
105: return head.element;
106: }
107:
108: private LinkElement next_enum = null;
109:
110: /**
111: * Starts an enumeration of all the elements in this list. Note that only
112: * one enumeration can be active at any time.
113: *
114: * @return the first element, or null if the list is empty
115: */
116: public synchronized Object enumerate() {
117: if (head == null)
118: return null;
119:
120: next_enum = head.next;
121: return head.element;
122: }
123:
124: /**
125: * Gets the next element in the enumeration. The enumeration must have
126: * been first initalized with a call to <code>enumerate()</code>.
127: *
128: * @return the next element, or null if none left
129: * @see #enumerate()
130: */
131: public synchronized Object next() {
132: if (next_enum == null)
133: return null;
134:
135: Object elem = next_enum.element;
136: next_enum = next_enum.next;
137:
138: return elem;
139: }
140:
141: public static void main(String args[]) throws Exception {
142: // LinkedList Test Suite
143:
144: System.err.println("\n*** Linked List Tests ...");
145:
146: LinkedList list = new LinkedList();
147: list.addToHead("One");
148: list.addToEnd("Last");
149: if (!list.getFirst().equals("One"))
150: throw new Exception("First element wrong");
151: if (!list.enumerate().equals("One"))
152: throw new Exception("First element wrong");
153: if (!list.next().equals("Last"))
154: throw new Exception("Last element wrong");
155: if (list.next() != null)
156: throw new Exception("End of list wrong");
157: list.remove("One");
158: if (!list.getFirst().equals("Last"))
159: throw new Exception("First element wrong");
160: list.remove("Last");
161: if (list.getFirst() != null)
162: throw new Exception("End of list wrong");
163:
164: list = new LinkedList();
165: list.addToEnd("Last");
166: list.addToHead("One");
167: if (!list.getFirst().equals("One"))
168: throw new Exception("First element wrong");
169: if (!list.enumerate().equals("One"))
170: throw new Exception("First element wrong");
171: if (!list.next().equals("Last"))
172: throw new Exception("Last element wrong");
173: if (list.next() != null)
174: throw new Exception("End of list wrong");
175: if (!list.enumerate().equals("One"))
176: throw new Exception("First element wrong");
177: list.remove("One");
178: if (!list.next().equals("Last"))
179: throw new Exception("Last element wrong");
180: list.remove("Last");
181: if (list.next() != null)
182: throw new Exception("End of list wrong");
183:
184: list = new LinkedList();
185: list.addToEnd("Last");
186: list.addToHead("Two");
187: list.addToHead("One");
188: if (!list.getFirst().equals("One"))
189: throw new Exception("First element wrong");
190: if (!list.enumerate().equals("One"))
191: throw new Exception("First element wrong");
192: if (!list.next().equals("Two"))
193: throw new Exception("Second element wrong");
194: if (!list.next().equals("Last"))
195: throw new Exception("Last element wrong");
196: if (list.next() != null)
197: throw new Exception("End of list wrong");
198: list.remove("Last");
199: list.remove("Two");
200: list.remove("One");
201: if (list.getFirst() != null)
202: throw new Exception("Empty list wrong");
203:
204: System.err.println("\n*** Tests finished successfuly");
205: }
206: }
207:
208: /**
209: * The represents a single element in the linked list.
210: */
211: class LinkElement {
212: Object element;
213: LinkElement next;
214:
215: LinkElement(Object elem, LinkElement next) {
216: this.element = elem;
217: this.next = next;
218: }
219: }
|