/* ListNode.java 1.0 */ /* originally from Georgia Tech's CS 1322 Module 27 notes */ /* on Linked Lists, Stacks, and Queues */ /* modified/adapted by Sharon Tuttle */ /* last modified: 2-27-03 */ public class ListNode implements Comparable { /*------------------------------------ fields -------------------------------------*/ // information we want to be in a linked list private Comparable data; // data must be of type Comparable --- // it must be of a class that either // implements Comparable, or it is a // subclass of a class that does so. // next node in the linked list private ListNode next; /*-------------------------------------- constructors ---------------------------------------*/ /* create a new node containing the desired data */ /* but not leading to any other node */ public ListNode (Comparable desiredData) { this.data = desiredData; this.next = null; } /*------------------------------------------------------- accessors -------------------------------------------------------*/ public Comparable getData() { return this.data; } public ListNode getNext() { return this.next; } /*------------------------------------------------------- modifiers -------------------------------------------------------*/ public void setData(Comparable newData) { this.data = newData; } public void setNext(ListNode newNext) { this.next = newNext; } /*----------------------------------------------------------- implementations of interface's abstract methods --------------------------------------------------------------*/ public int compareTo(Object obj) { // yes, this is clunky. compareTo() must conform to the // given interface, but I cannot really compare it unless // it is a ListNode object. So, I cast the parameter object // to that type, which should work because I only plan to // call this for ListNode objects comparing themselves to // OTHER ListNode objects... ListNode objNode = (ListNode) obj; // get the data from the passed (assumed) ListNode Comparable objData = objNode.getData(); // data must be of type Comparable --- so I know it, too, supports // method compareTo()... return ( (this.data).compareTo(objData) ); } /*------------------------------------------ overridden methods -------------------------------------------*/ public String toString() { /* hmm; I don't think I'll print next in list... */ return "ListNode[" + this.data + "]"; } } // end of class ListNode