02. Implementing a Node

We'll be using Java Generics in our guide here, so it's highly recommended to learn that before moving on.

Implementing a Node is simple. It should store a generic element along with get and set methods to retrieve and mutate that element.

Additionally, since we are using this for a singly-linked list, it should have a next pointer, which points to the next node in the sequence.

/** 
 *  A Node object implementation to hold data in a LinkedList.
 *  
 *  @author  CodeSnipcademy
 *  @version  1.0.0 Jan 07, 2015
 */
public class Node<E> {
  private Node<E> next;   // Points to the next Node
  private E data;         // Holds data to be     | stored in Node
 
  /**
   * Create a new node with no next Node and no     | data.
   */
  public Node() {
    this(null, null);
  }
 
  /** 
   * Create a new Node with given data.
   *
   * @param  inputData - element to be stored
   */
  public Node(E inputData) {
    this(inputData, null);  
  }
 
  /** 
   * Create a new Node with data at the given index.
   * 
   * @param  inputData - element to be stored
   * @param  inputNext - next Node to be pointed to 
   */ 
  public Node(E inputData, Node<E> inputNext) {
    this.next = inputNext;
    this.data = inputData;
  }
 
  /**
   * Retrieve data of current Node.
   * @return  data - data stored in this Node
   */     
  public E getData() {
    return this.data;
  }
 
  /**
   * Set new data to Node.
   * @param  inputData - data stored in this Node
   */     
  public void setData(E inputData) {
    this.data = inputData;
  }
 
  /**
   * Return the next Node in the sequence.
   * @return  next - next data in the Node
   */     
  public Node<E> getNext() {
    return this.next;
  }
 
  /**
   * Set next value in current Node.
   * @param  inputNode - next Node to be set
   */     
  public void setNext(Node<E> inputNode) {
    this.next = inputNode;
  }
}

Want to avoid becoming a code monkey?

The Productive Programmer

Want to avoid becoming a code monkey? Try Good Practice

The Productive Programmer offers critical timesaving and productivity tools that you can adopt right away. The book offers advice on the mechanics of productivity - how to work smarter, spurn interruptions, get the most out your computer, and avoid repetition - along with valuable practices that will help you elude common traps, improve your code, and become more valuable to your team.

$ Check price
39.9939.99Amazon 4 logo(41+ reviews)

More Good Practice resources

Learn Enterprise Java Development for a Bright Career

Effective Java

Learn Enterprise Java Development for a Bright Career Try Java

Are you looking for a deeper understanding of the Java so that you can write code that is clearer, more correct, more robust, and more reusable? Look no further! Effective Java brings together seventy-eight indispensable programmer's rules of thumb: working, best-practice solutions for the programming challenges you encounter every day.

$ Check price
54.9954.99Amazon 4.5 logo(219+ reviews)

More Java resources

Ad