Contents    Page-10    Prev    Next    Page+10    Index   

Constructing a Linked List

A linked list is usually made by linking new elements onto the front of the list. The function (cons item list ) makes a new list element containing item and adds it to the front of list:

(cons 'a nil)              ->  (a)
(cons 'a '())
cons("a", null)
cons("a", list())

(cons 'a '(b c))           ->  (a b c)
cons("a", list("b", "c"))

In Java, the class Cons is:

public class Cons  {
    private Object car;
    private Cons cdr;

    private Cons(Object first, Cons rest)
       { car = first;
         cdr = rest; }
    public static Cons
                  cons(Object first, Cons rest)
      { return new Cons(first, rest); }