Package cs1302.adt

Class Node

java.lang.Object
cs1302.adt.Node

public class Node extends Object
A node for an item in a linked list data structure. Each node contains an item and may refer to another node in the structure (known as the "next" node).
UML Diagram for Node

Examples

In this example, we construct a linked list using two newly-created Node objects. The second node becomes the new head of the linked list.
 Node head = new Node("a");   // head → Node(a, null)
 head = new Node("b", head);  // head → Node(b) → Node(a, null)
 System.out.printf("head → %s\n", head.asString());
 
 head → Node(b) → Node(a, null)
 
In this example, we construct a linked list using two newly-created Node objects. The second node becomes the new tail of the linked list.
 Node head = new Node("a");   // head → Node(a, null)
 head.setNext(new Node("b")); // head → Node(a) → Node(b, null)
 System.out.printf("head → %s\n", head.asString());
 
 head → Node(a) → Node(b, null)
 
In this example, we construct a linked list using three newly-created Node objects. The first and third objects become the head and tail of the linked list, respectively.
 Node node1 = new Node("a");    // node1 → Node(a, null)
 Node node2 = new Node("b");    // node2 → Node(b, null)
 Node node3 = new Node("c");    // node3 → Node(c, null)
 Node head = node1;             //  head → Node(a, null)
 head.setNext(node2);           //  head → Node(a) → Node(b, null)
 head.getNext().setNext(node3); //  head → Node(a) → Node(b) → Node(c, null)
 System.out.printf("head → %s\n", head.asString());
 
 head → Node(a) → Node(b) → Node(c, null)
 

Very Important Note

You do not need to write the .java file for this class (nor should you)!

A compiled version of this Node class is made available to you in a JAR file that is distributed alongside the project description. Simply follow the instructions in that document to get and use the JAR file.

Author:
Michael E. Cotterell
  • Constructor Details

    • Node

      public Node(String item, Node next)
      Construct a node with the specified item and next.
      Parameters:
      item - the item to be contained in this node
      next - the next node in the stnructure
    • Node

      public Node(String item)
      Construct a node with the specified item but no next node.
      Parameters:
      item - the item to be contained in this node
  • Method Details

    • getItem

      public String getItem()
      Returns the item in this node.
      Returns:
      the item in this node
    • setItem

      public void setItem(String item)
      Sets the item in this node.
      Parameters:
      item - the item in this node
    • getNext

      public Node getNext()
      Returns the next node.
      Returns:
      the next in this node, if available; otherwise, null
    • setNext

      public void setNext(Node next)
      Sets the next node.
      Parameters:
      next - the next node
    • hasNext

      public boolean hasNext()
      Returns true when the next node is set.
      Returns:
      true when getNext() != null; false otherwise
    • asString

      public String asString()
      Returns a string representation of the linked list starting with this node. Here are some examples of strings produced by this method:
       "Node(a, null)"
       "Node(a) → Node(b, null)"
       "Node(a) → Node(b) → Node(c, null)"
       "Node(a) → Node(b) ↻ Node(b) ⋯"           // cycle: second node refers to itself!
       "Node(a) → Node(b) → Node(c) ↻ Node(b) ⋯" // cycle: last node refers to second node!
       
      This method is intentionally named asString so that the usual implementation of the toString method is still available for debugging purposes.
      Returns:
      a string representation of the linked list