Skip to content
Advertisement

Is it possible to use 2 Data Type in an Custom ADT(Sorted Linked List)?

I am trying to do a Leaderboard for a game by using a Sorted Linked List. I was able to do so by sorting the point in descending order which mean higher point to lower point. Moreover, I will also need to put player name together with the point. The problem comes here. The SLL(Sorted Linked List) I implemented is an Integer data type, it works perfectly with the Integer data type as it sorted the numbers.

JavaScript

But when I trying to put the player name which used String, it won’t be able to do so because the point data type will need to follow the player name’s data type. Below are the codes of the driver class:

JavaScript

Here is the Entity class

JavaScript

Here’s the custom Sorted Lineked List

JavaScript

And the results is here

JavaScript

Here’s my testing on the point with string data type because I can’t think a way to use player name and point with 2 different data types at the same time in my custom ADT.

JavaScript

And here’s the result that it compare the first letter of the string.

JavaScript

Is there anyway to use both String and Integer in one ADT because if i couldn’t do so, I won’t be able sort the point. I’m so sorry for the long question. I am very new to data structures and algorithms so I really need some help on this. Much appreciate.

Advertisement

Answer

To keep the points and names together, you need to add Player objects into the list as they are, not just their points or their names:

JavaScript

For this to work, you will need to be able to compare Player objects by their point numbers. You do that by implementing the Comparable interface and adding a compareTo method.

You’ll also want to add a toString() method, so that you can print a Player object.

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement