next up previous index
Next: List Implementation of Stack Up: A List Node Class Previous: A List Node Class   Index

The ListNode

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

\begin{picture}(440,60)(40,0)
\put(215,49){\makebox(0,0)[b]{\texttt{data}}}
\p...
...}}
\put(240,0){\framebox (40,40)}
\put(260,20){\vector(1,0){60}}
\end{picture}
where the data field stores the contents of the node and the next field stores a reference to the next node.



Peter Williams 2005-06-07