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

Posted on 2003-03-14
Medium Priority
Last Modified: 2010-03-31
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); }
          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; }
Question by:grivad
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 92

Accepted Solution

objects earned 225 total points
ID: 8140469
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.

Author Comment

ID: 8140676
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!

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month9 days, 19 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question