Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 198
  • Last Modified:

Generic LinkedList using Comparable...? - can't afford extra points, but important! please help!!

I'm trying to write a LinkedList class which is generic (ie: uses type Object), but one of my methods has a compareTo() call (from Comparable)...when I compile, it tells me that (naturally) there is no compareTo() method in type Object.  How would I go about this?  Where would I add Comparable and implement compareTo()??

Here is my code for the list/nodes:

public class LinkedList {
     private Node first = new Node();
     private Node current = first;
     private Node pre = null;
     
     public boolean isEmpty() { return (first.getNext() == null); }

     public boolean inList(Object obj) {
          current = first;
          pre = null;
          while((current != null) && current.getData().compareTo(obj) > 0) {
               pre = current;
               current = current.getNext();
          }
          return (current != null) && (current.getData().compareTo(obj) == 0);
     }
     
     public Object peekAt() { return current.getData(); }
     
     public void insert(Object obj) {
          Node temp = new Node(obj);
          if(pre == null) { first = temp; }
          else { pre.setNext(temp); }
          temp.setNext(current);
          current = temp;
     }
     
     public void remove(Object obj) throws RunTimeException {
          if(!inList(obj)) { throw new RunTimeException("Item not found"); }
          else {
               current = current.getNext();
               if(pre == null) { first = current; }
               else { pre.setNext(current); }
          }
     }
     
     public void delete() {
          current = current.getNext();
          if(pre == null) { first = current; }
          else { pre.setNext(current); }
     }
     
     public void traverse() {
          Node temp = first;
          while(temp != null) { System.out.prinln(temp.getData().toString()); }
          temp = temp.getNext();
     }
     
     public void clear() {
          first = new Node();
          current = first;
          pre = null;
     }
}


public class Node {
     
     private Object data;
     private Node next;

     public Node() { next = null; }

     public Node(Object obj) { data = obj; next = null; }

     public Node(Object obj, Node node) { data = obj; next = node; }
 
     public void setData(Object obj) { data = obj; }

     public void setNext(Node node) { next = node; }

     public Object getData() { return data; }

     public Node getNext() { return next; }
}
0
grivad
Asked:
grivad
1 Solution
 
objectsCommented:
You can't really, if you want to use compareTo() you should only allow Comparable objects to be added to your list. You could instead provide a Comparator to your list and it could use that instead of compareTo().

And looking at your code, I'm not sure I understand what you are using it for. The inList() method seems to require that the data in the list is sorted, which it may not be.
0
 
grivadAuthor Commented:
Thanks, I already figured that out (using Comparable instead).. I just wanted to use Object to ensure genericity..

The idea of inList is that it will be called before every insert call, thus maintaining order.. now if somehow insert got called without calling inList first, yeah, there would be no order..

Thanks for your help!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now