Package cs1302.gen

Class Node<Type>

java.lang.Object
cs1302.gen.Node<Type>
Type Parameters:
Type - the item type

public class Node<Type> 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).

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<String> head = new Node<String>("a"); // head -> Node(a, null)
head = new Node<String>("b", head);        // head -> Node(b) -> Node(a, null)
System.out.printf("head -> %s\n", Node.asString(head));
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<String> head = new Node<String>("a"); // head -> Node(a, null)
head.setNext(new Node("b"));               // head -> Node(a) -> Node(b, null)
System.out.printf("head -> %s\n", Node.asString(head));
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<String> node1 = new Node<>("a"); // node1 -> Node(a, null)
Node<String> node2 = new Node<>("b"); // node2 -> Node(b, null)
Node<String> node3 = new Node<>("c"); // node3 -> Node(c, null)
Node<String> 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", Node.asString(head));
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.

  • Constructor Details

    • Node

      public Node(Type item, Node<Type> 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 structure
    • Node

      public Node(Type 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 Type getItem()
      Returns the item in this node.
      Returns:
      the item in this node
    • setItem

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

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

      public void setNext(Node<Type> 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 static <Type> String asString(Node<Type> node, Function<Type,String> toString)
      Returns a string representation of the linked list starting with this node. Here are some examples of strings produced by this method, assuming Object::toString is supplied for the toString parameter:
      "Node(a, null)"
      "Node(a) -> Node(b, null)"
      "Node(a) -> Node(b) -> Node(c, null)"
      If a cycle is detected, then @> is to denote that a node's "next" refers to a node that has already been included in the string:
      // second node refers to itself!
      "Node(a) -> Node(b) @> Node(b) ~"
      // last node refers to second node!
      "Node(a) -> Node(b) -> Node(c) @> Node(b) ~"
      This method is intentionally named asString so that the usual implementation of the toString method is still available for debugging purposes. Callers can use the toString parameter to customize the resulting string to some degree, which may be convenient in cases where the item type, Type, does not override the toString in a desirable way or at all. In the examples above, Object::toString is supplied for toString, which is roughly equivalent to using the following lambda expression:
      (item) -> {
          return item.toString();
      }
      Type Parameters:
      Type - the item type.
      Parameters:
      node - the node to start with.
      toString - a function that maps an item to a string.
      Returns:
      a string representation of the linked list.
      Throws:
      NullPointerException - if node or toString is null.
    • asString

      public static <Type> String asString(Node<Type> node)
      Returns a string representation of the linked list starting with this node using Node.asString(node, Object::toString).
      Type Parameters:
      Type - the item type.
      Parameters:
      node - the node to start with.
      Returns:
      Node.asString(node, Object::toString)
      Throws:
      NullPointerException - if node is null.
      See Also: