home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / VCSAMPL.BIN / LinkList.java < prev    next >
Text File  |  1997-08-23  |  944b  |  59 lines

  1. /**
  2.  * A simple one-directional external linked list class.
  3.  */
  4.  
  5. public class LinkList extends Object
  6. {
  7.     private LinkList therest;
  8.     private Object thevalue;
  9.  
  10.     public LinkList(Object obj)
  11.     {
  12.         therest = null;
  13.         thevalue = obj;
  14.     }
  15.  
  16.     /**
  17.      * Create a new list node; insert it at the head of the list. Return the list.
  18.      */
  19.  
  20.     public LinkList cons(Object obj)
  21.     {
  22.         LinkList newnode = new LinkList(obj);
  23.         newnode.thevalue = thevalue;
  24.         newnode.therest = therest;
  25.         therest = newnode;
  26.         thevalue = obj;
  27.         return this;
  28.     }
  29.  
  30.     /**
  31.      * The object owned by the list node
  32.      */
  33.  
  34.     public Object value()
  35.     {
  36.         return thevalue;
  37.     }
  38.  
  39.     /**
  40.      * The remainder of the list
  41.      */
  42.  
  43.     public LinkList rest()
  44.     {
  45.         return therest;
  46.     }
  47.  
  48.     /**
  49.      * Swap the values of a and b.
  50.      */
  51.     public static void swapValues(LinkList a, LinkList b)
  52.     {
  53.         Object save = a.thevalue;
  54.         a.thevalue = b.thevalue;
  55.         b.thevalue = save;
  56.     }
  57. }
  58.  
  59.