home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 21 / IOPROG_21.ISO / SOFT / JSL.ZIP / JSL20 / simula / simset / Head.java < prev    next >
Encoding:
Java Source  |  1998-02-20  |  1.8 KB  |  93 lines

  1.  
  2. package simula.simset;
  3.  
  4. import simula.SimulaException;
  5.  
  6. /**
  7.  * This class implements a double-linked list.
  8.  */
  9. public class Head extends Linkage
  10. {
  11.     // --------------------------- VARIABLES --------------------------
  12.     private Link first;
  13.     private Link last;
  14.  
  15.  
  16.     // --------------------------- CONSTRUCTOR ------------------------
  17.     /**
  18.      * Initialize an empty list.
  19.      */
  20.     public Head() {
  21.         first = null;
  22.         last = null;
  23.     }
  24.     /**
  25.      * Returns the number of elements of the list.
  26.      * @return the number of elements of the list.
  27.      */
  28.     public int cardinal() {
  29.         int count = 0;
  30.         Link current;
  31.  
  32.         if (this.empty()) {
  33.             return 0;
  34.         } else {
  35.             current = first;
  36.  
  37.             while (current != null) {
  38.                 count++;
  39.                 current = (Link) current.suc();
  40.             }
  41.  
  42.             return count;
  43.         }
  44.     }
  45.     /**
  46.      * Clears the list.
  47.      */
  48.     public void clear() {
  49.         first = null;
  50.         last = null;
  51.         pred = null;
  52.         suc = null;
  53.     }
  54.     /**
  55.      * Returns <code>True</code> if the list is empty, <code>False</code>
  56.      * otherwise
  57.      * @return True if the list is empty.
  58.      */
  59.     public boolean empty() {
  60.         return (first == null && last == null);
  61.     }
  62.     // --------------------------- METHODS ------------------------
  63.     /**
  64.      * Returns the first element of the list.
  65.      * @return the first element of the list.
  66.      */
  67.     public Link first() {
  68.         return first;
  69.     }
  70.     /**
  71.      * Sets the first element of the list.
  72.      * @param _first the new element to be first.
  73.      */
  74.     protected void first(Link _first) {
  75.         first = _first;
  76.         suc = (Linkage) _first;
  77.     }
  78.     /**
  79.      * Returns the last element of the list.
  80.      * @return the last element of the list.
  81.      */
  82.     public Link last() {
  83.         return last;
  84.     }
  85.     /**
  86.      * Sets the last element of the list.
  87.      * @param _last the new element to be last.
  88.      */
  89.     protected void last(Link _last) {
  90.         last = _last;
  91.         pred = (Linkage) _last;
  92.     }
  93. } // end of class Head