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 167655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:18:50+00:00 2026-05-11T12:18:50+00:00

I wrote a linked list implementation for my java class earlier this year. This

  • 0

I wrote a linked list implementation for my java class earlier this year. This is a generic class called LList. We now have to write out the merge sort algorithm for a lab. Instead of creating a new List implementation that takes Ints, I decided to just reuse the generic list I had created before.

The problem is how do I compare two generic objects? java wont let me do something like

if(first.headNode.data > second.headNode.data) 

So, my question is, is their a way to implement some sort of comparison function that will work on any type of data? I tried the following:

        String one, two;         one = first.headNode.data.toString();         two = second.headNode.data.toString();         if(first.headNode.data.compareTo(second.headNode.data) < 0) {             result.add(first.headNode.data);             // remove head node. remove() takes care of list size.             first.remove(1);         } else {             // If compareTo returns 0 or higher, second.headNode is lower or             // equal to first.headNode. So it's safe to update the result             // list             result.add(second.headNode.data);             second.remove(1);         } 

Which wont even work properly. I tested with the numbers 6 and 12, the above adds 12 to the result list.

Relevant stuff:

 private LList<T> mergeSort(LList<T> list) {     LList<T> first = new LList();     LList<T> second = new LList();     if (list.length() == 1) {         return list;     }      int middle = list.length() / 2;     second.headNode = list.getNodeAt(middle + 1);     second.length = list.length() - (middle);     // Set first half to full list, then remove the 'second' half.     first.headNode = list.headNode;     first.length = middle;     first.getNodeAt(middle).next = null;      // Get the splitted halves.     first = mergeSort(first);     second = mergeSort(second);     return merge(first, second); }  private LList<T> merge(LList<T> first, LList<T> second) {     LList<T> result = new LList();      while((first.length > 0) && (second.length > 0)) {         // Ok, lets force toString to compare stuff since generics are a pain.         String one, two;         one = first.headNode.data.toString();         two = second.headNode.data.toString();         if(one.compareTo(two)) < 0) {             result.add(first.headNode.data);             // remove head node. remove() takes care of list size.             first.remove(1);         } else {             // If compareTo returns 0 or higher, second.headNode is lower or             // equal to first.headNode. So it's safe to update the result             // list             result.add(second.headNode.data);             second.remove(1);         }     }     return result; } 

NOTE: entire LList class can be found [here](http://rapidshare.com/files/219112739/LList.java.html MD5: BDA8217D0756CC171032FDBDE1539478)

  • 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. 2026-05-11T12:18:50+00:00Added an answer on May 11, 2026 at 12:18 pm

    Look into the Comparator and Comparable interfaces.

    Your sort method should take Comparator or you should specify < T extends Comparable > so that the Comparable interface can be used.

    public void sort(Comparable<T> comparator) {     sort(SortType.MERGE, comparator); } .... private LList<T> merge(LList<T> first, LList<T> second) {     ...         if(comparator.compare(first.headNode.data, second.headNode.data) < 0) {     ... } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer First off, make sure the WebService is set to Dynamic.… May 11, 2026 at 11:49 pm
  • Editorial Team
    Editorial Team added an answer You want them to be easily editable which means you… May 11, 2026 at 11:49 pm
  • Editorial Team
    Editorial Team added an answer I would recommend you take a look at System.ComponentModel.TypeConverter. This… May 11, 2026 at 11:49 pm

Related Questions

when programming in Java I practically always, just out of habit, write something like
Windows provides a lockless singly linked list, as documented at this page: Win32 SList
Apologies for duplicate posting. Hi I am having trouble marshalling a linked list from
For my programming class I have to write a linked list class. One of

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.