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.