java.lang.Object
cs1302.gen.Node<Type>
- Type Parameters:
Type
- the item type
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-createdNode
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <Type> String
Returns a string representation of the linked list starting with this node using Node.asString(node, Object::toString).static <Type> String
Returns a string representation of the linked list starting with this node.getItem()
Returns the item in this node.getNext()
Returns the next node.boolean
hasNext()
Returnstrue
when the next node is set.void
Sets the item in this node.void
Sets the next node.
-
Constructor Details
-
Node
Construct a node with the specifieditem
andnext
.- Parameters:
item
- the item to be contained in this nodenext
- the next node in the structure
-
Node
Construct a node with the specifieditem
but no next node.- Parameters:
item
- the item to be contained in this node
-
-
Method Details
-
getItem
Returns the item in this node.- Returns:
- the item in this node
-
setItem
Sets the item in this node.- Parameters:
item
- the item in this node
-
getNext
Returns the next node.- Returns:
- the next in this node, if available; otherwise,
null
-
setNext
Sets the next node.- Parameters:
next
- the next node
-
hasNext
public boolean hasNext()Returnstrue
when the next node is set.- Returns:
true
whengetNext() != null
;false
otherwise
-
asString
Returns a string representation of the linked list starting with this node. Here are some examples of strings produced by this method, assumingObject::toString
is supplied for thetoString
parameter:"Node(a, null)"
"Node(a) -> Node(b, null)"
If a cycle is detected, then"Node(a) -> Node(b) -> Node(c, null)"
@>
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) ~"
This method is intentionally named// last node refers to second node! "Node(a) -> Node(b) -> Node(c) @> Node(b) ~"
asString
so that the usual implementation of thetoString
method is still available for debugging purposes. Callers can use thetoString
parameter to customize the resulting string to some degree, which may be convenient in cases where the item type,Type
, does not override thetoString
in a desirable way or at all. In the examples above,Object::toString
is supplied fortoString
, 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
- ifnode
ortoString
isnull
.
-
asString
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
- ifnode
isnull
.- See Also:
-