Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6038821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:16:41+00:00 2026-05-23T06:16:41+00:00

I am implementing a sorted list using linked lists. My node class looks like

  • 0

I am implementing a sorted list using linked lists. My node class looks like this

public class Node<E>{
    E elem;
    Node<E> next, previous;
}

In the sorted list class I have the add method, where I need to compare generic objects based on their implementation of compareTo() methods, but I get this syntax error
“The method compareTo(E) is undefined for type E”. I have tried implemnting the compareTo method in Node, but then I can’t call any of object’s methods, because E is generic type.
Here is the non-finished body of add(E elem) method.

public void add(E elem) 
{

        Node<E> temp = new Node<E>();
        temp.elem = elem;

        if( isEmpty() ) {           
            temp.next = head;
            head.previous = temp;
            head = temp;
            counter++; 
        }else{
            for(Node<E> cur = head; cur.next != null ; cur= cur.next) {
                **if(temp.elem.comparTo(cur.elem)) {**
                    //do the sort;

                }/*else{
                    cur.previous = temp;
                }*/             
            }
            //else insert at the end

        }
}

Here is one of the object implemnting compareTo method

public class Patient implements Comparable<Patient>{
    public int compareTo(Patient that)
    {
        return (this.getPriority() <= that.getPriority() ? 1 : 0 );
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:16:42+00:00Added an answer on May 23, 2026 at 6:16 am

    Bound E to Comparable:

    public class Node<E extends Comparable<E>>{
        E elem;
        Node<E> next, previous;
    }
    

    It will compile now.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have completed implementing Operation of Linear Linked List using C , Now inorder
I got a problem here, implementing a Sorted Doubly Linked List of first and
This is similar to a recent question. I will be maintaining sorted a list
Let's start with this overload of List BinarySearch: public int BinarySearch(T item, IComparer<T> comparer);
I have a problem implementing this example in java, tableFilterDemo is a class that
I have an array with a list of objects sorted alphabetically ignoring the letters
I'm implementing a bitonic sort using intel TBB. When using the parallel_invoke method all
Implementing custom DataAnnotationsModelMetadataProvider in ASP.NET MVC2. Assuming the object that is being rendered looks
I`m implementing a custom filesystem on Ubuntu using Fuse, but I need to trap
I'm having some problems with the stack in java... I'm implementing quicksort using an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.