/** * A simple Node class for implementing linked structures. An * interesting design question is whether we permit setting of * fields (we could instead require the client to create a new * replacement node). * * @author Samuel A. Rebelsky * @version 1.0 of April 1999 */ public class Node { // +--------+-------------------------------------------------- // | Fields | // +--------+ /** The contents of the current node. */ protected Object contents; /** The next node in the list. Set to null for empty lists. */ protected Node next; // +--------------+-------------------------------------------- // | Constructors | // +--------------+ /** * Create a new node with specified contents and no next * element. */ public Node(Object contents) { this(contents,null); } // Node(Object) /** * Create a new node with specified contents and specified * next element. The next element can be null. */ public Node(Object contents, Node next) { this.contents = contents; this.next = next; } // Node(Object,Node) // +-----------+----------------------------------------------- // | Accessors | // +-----------+ /** * Get the contents of the current node. */ public Object getContents() { return this.contents; } // getContents() /** * Get the next node. */ public Node getNext() { return this.next; } // getNext() // +-----------+----------------------------------------------- // | Modifiers | // +-----------+ /** * Set the contents of the current node. Returns the old contents. */ public Object setContents(Object newContents) { Object oldContents = this.contents; this.contents = newContents; return oldContents; } // setContents(Object) /** * Set the next node. Returns the old next node. */ public Object setNext(Node newNext) { Object oldNext = this.next; this.next = newNext; return oldNext; } // setNext(Node) } // class Node