Program 21.5: A linked list node

Program 21.5 is a node for a basic linked list.

public class Node {

  Object data;
  Node next;
        
  Node(Object o) { 
    data = o;
    next = null;
  }

  Node getNext() {
  
    return next;
  
  }

  public void setNext(Node n) {
  
    n.next = next;
    next = n;
  
  }

}
The main thing to notice is that instead of having a pointer to the next Node, you just use the next node itself. Of course itıs really just a pointer so nothing is lost and some clarity is gained.


Copyright 1996 Elliotte Rusty Harold
elharo@sunsite.unc.edu
This Chapter
Examples
Home