next up previous
Next: Creation and linking of Up: Unit 12 Previous: Linked lists

The class ListNode

The basic class for a linked lists is a class whose objects represent the information associated to a single element (or node) of the structure.

public class ListNode {
  public ElementType info;
  public ListNode next;
}

Such a class defines two public instance variables:

In the case where we need a list only as an auxiliary data structure for a certain class C, we can also define the class ListNode in the same file C.java in which we define the class C, as follows:

class ListNode {
  ElementType info;
  ListNode next;
}

public class C {
  ...
}

Since the class ListNode and its instance variables info and next are not declared public (or private), the visibility of ListNode and of its instance variables is at the level of package (i.e., it is visible only to classes defined in the same directory in which the file C.java is placed). Note that, if instead we define ListNode as a public class, we must place it in a separate file ListNode.java.

Note that, in both cases (when the class list is public, and when it is visible at the level of package), the instance variables info and next are not declared as private, because we want to directly access them outside the class ListNode in order to manipulate lists (the class ListNode itself has no methods of its own, besides the default constructor without parameters).


next up previous
Next: Creation and linking of Up: Unit 12 Previous: Linked lists