LinkedList.java
Upload User: demmber
Upload Date: 2007-12-22
Package Size: 717k
Code Size: 6k
Category:

Java Develop

Development Platform:

Java

  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鋜
  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. package HTTPClient;
  33. /**
  34.  * This class implements a singly linked list.
  35.  *
  36.  * @version 0.3-3  06/05/2001
  37.  * @author Ronald Tschal鋜
  38.  */
  39. class LinkedList
  40. {
  41.     /** head of list */
  42.     private LinkElement head = null;
  43.     /** tail of list (for faster adding) */
  44.     private LinkElement tail = null;
  45.     /**
  46.      * Add the specified element to the head of the list.
  47.      *
  48.      * @param elem the object to add to the list.
  49.      */
  50.     public synchronized void addToHead(Object elem)
  51.     {
  52. head = new LinkElement(elem, head);
  53. if (head.next == null)
  54.     tail = head;
  55.     }
  56.     /**
  57.      * Add the specified element to the end of the list.
  58.      *
  59.      * @param elem the object to add to the list.
  60.      */
  61.     public synchronized void addToEnd(Object elem)
  62.     {
  63. if (head == null)
  64.     head = tail = new LinkElement(elem, null);
  65. else
  66.     tail = (tail.next = new LinkElement(elem, null));
  67.     }
  68.     /**
  69.      * Remove the specified element from the list. Does nothing if the element
  70.      * is not in the list.
  71.      *
  72.      * @param elem the object to remove from the list.
  73.      */
  74.     public synchronized void remove(Object elem)
  75.     {
  76. if (head == null)  return;
  77. if (head.element == elem)
  78. {
  79.     head = head.next;
  80.     return;
  81. }
  82. LinkElement curr = head;
  83. while (curr.next != null)
  84. {
  85.     if (curr.next.element == elem)
  86.     {
  87. if (curr.next == tail)  tail = curr;
  88. curr.next = curr.next.next;
  89. return;
  90.     }
  91.     curr = curr.next;
  92. }
  93.     }
  94.     /**
  95.      * Return the first element in the list. The list is not modified in any
  96.      * way.
  97.      *
  98.      * @return the first element
  99.      */
  100.     public synchronized Object getFirst()
  101.     {
  102. if (head == null)  return null;
  103. return head.element;
  104.     }
  105.     private LinkElement next_enum = null;
  106.     /**
  107.      * Starts an enumeration of all the elements in this list. Note that only
  108.      * one enumeration can be active at any time.
  109.      *
  110.      * @return the first element, or null if the list is empty
  111.      */
  112.     public synchronized Object enumerate()
  113.     {
  114. if (head == null)  return null;
  115. next_enum = head.next;
  116. return head.element;
  117.     }
  118.     /**
  119.      * Gets the next element in the enumeration. The enumeration must have
  120.      * been first initalized with a call to <code>enumerate()</code>.
  121.      *
  122.      * @return the next element, or null if none left
  123.      * @see #enumerate()
  124.      */
  125.     public synchronized Object next()
  126.     {
  127. if (next_enum == null)  return null;
  128. Object elem = next_enum.element;
  129. next_enum = next_enum.next;
  130. return elem;
  131.     }
  132.     public static void main(String args[])  throws Exception
  133.     {
  134. // LinkedList Test Suite
  135. System.err.println("n*** Linked List Tests ...");
  136. LinkedList list = new LinkedList();
  137. list.addToHead("One");
  138. list.addToEnd("Last");
  139. if (!list.getFirst().equals("One"))
  140.     throw new Exception("First element wrong");
  141. if (!list.enumerate().equals("One"))
  142.     throw new Exception("First element wrong");
  143. if (!list.next().equals("Last"))
  144.     throw new Exception("Last element wrong");
  145. if (list.next() != null)
  146.     throw new Exception("End of list wrong");
  147. list.remove("One");
  148. if (!list.getFirst().equals("Last"))
  149.     throw new Exception("First element wrong");
  150. list.remove("Last");
  151. if (list.getFirst() != null)
  152.     throw new Exception("End of list wrong");
  153. list = new LinkedList();
  154. list.addToEnd("Last");
  155. list.addToHead("One");
  156. if (!list.getFirst().equals("One"))
  157.     throw new Exception("First element wrong");
  158. if (!list.enumerate().equals("One"))
  159.     throw new Exception("First element wrong");
  160. if (!list.next().equals("Last"))
  161.     throw new Exception("Last element wrong");
  162. if (list.next() != null)
  163.     throw new Exception("End of list wrong");
  164. if (!list.enumerate().equals("One"))
  165.     throw new Exception("First element wrong");
  166. list.remove("One");
  167. if (!list.next().equals("Last"))
  168.     throw new Exception("Last element wrong");
  169. list.remove("Last");
  170. if (list.next() != null)
  171.     throw new Exception("End of list wrong");
  172. list = new LinkedList();
  173. list.addToEnd("Last");
  174. list.addToHead("Two");
  175. list.addToHead("One");
  176. if (!list.getFirst().equals("One"))
  177.     throw new Exception("First element wrong");
  178. if (!list.enumerate().equals("One"))
  179.     throw new Exception("First element wrong");
  180. if (!list.next().equals("Two"))
  181.     throw new Exception("Second element wrong");
  182. if (!list.next().equals("Last"))
  183.     throw new Exception("Last element wrong");
  184. if (list.next() != null)
  185.     throw new Exception("End of list wrong");
  186. list.remove("Last");
  187. list.remove("Two");
  188. list.remove("One");
  189. if (list.getFirst() != null)
  190.     throw new Exception("Empty list wrong");
  191. System.err.println("n*** Tests finished successfuly");
  192.     }
  193. }
  194. /**
  195.  * The represents a single element in the linked list.
  196.  */
  197. class LinkElement
  198. {
  199.     Object      element;
  200.     LinkElement next;
  201.     LinkElement(Object elem, LinkElement next)
  202.     {
  203. this.element = elem;
  204. this.next    = next;
  205.     }
  206. }