We shall store a new item in a structure, or record, with two elements. In Java we have to define a class for this purpose. Here is one that will serve.
class ListNode {
Object data;
ListNode next;
ListNode(Object data, ListNode next) {
this.data = data;
this.next = next;
}
}
The ListNode class has two instance variables, data which
is of type Object and next which is also of type ListNode. There is just a single constructor which asks for the
values to be stored in the two instance variables. You can picture a
list node as a pair looking like
where the data field stores the contents of the node and the next field stores a reference to the next node.
![]()