I'm creating my own linklist data structure in java where each node in the linked list will hold an object of a another class (car class for eg).So it'll basically be a linked list of cars.
public class car {
private String carName;
private double price;
car(String name,double price){
this.carName=name;
this.price=price;
}
public int compareTo(car newCar)
{
return this.carName.compareTo(newCar.carName);
}
}
Now I am trying to make the link list generic so that it can also be a Linklist
of integers,strings or other objects.
customLinklist<cars> newList=customLinklist<cars>()
customLinklist<String> newList=customLinklist<String>()
customLinklist<float> newList=customLinklist<float>()
The LinkList
is sorted alphabetically so when I add a car to the list it'll be inserted in a sorted manner.
In the ADD function in the Linklist
I compare the objects(which are wrapped in the nodes) to find out the location where I have to add the new object/node
This is my CustomLinklist class and Node Class
public class CustomLinkedList<E> {
public class Node<E>{
private E data;
private Node<E> next;
private Node<E> back;
public int compareTo(Node<E> newNode){
return this.data.compareTo(newNode.data);
}
}
private Node<E> head,tail;
CustomLinkedList(){
head=null;
tail=null;
}
public void add(E newObject){
if (head==null)
{
head=new Node<E>();
head.data=newObject;
head.next=null;
head.back=null;
tail=head;
}
else
{
Node<E> currentNode=head,newNode = null;
Node<E> prevNode;
newNode=new Node<E>();
newNode.data=newObject;
newNode.next=null;
newNode.back=null;
while( currentNode.compareTo(newNode)<0 && currentNode.next!=null)
{
currentNode=currentNode.next;
}
if(currentNode.next==null && currentNode!=head)
{
currentNode.next=newNode;
newNode.back=currentNode;
tail=newNode;
}
else
{
prevNode=currentNode.back;
if(currentNode!=head)
{
prevNode.next=newNode;
}
newNode.back=prevNode;
newNode.next=currentNode;
currentNode.back=newNode;
if(currentNode==head)
{
head=newNode;
}
}
}
}
}
Now the comparison logic I want to keep in the object class( car class) so that any changes the made to be made in the future( if i want to compare prices for eg) will be made to the car class .
Since I'm trying to make it generic,so that I can use the Linklists
to hold other objects( which will also have the compareTo function) ,this part of the code:
public int compareTo(Node<E> newNode){
return this.data.compareTo(newNode.data);
}
does not allow call the compareTo
function and gives the error "The method compareTo(E)
is undefined for the type E"
How can I make it generic so that I can call the compare function without knowing the type of the object?
Basically you have to make sure that the elements in your linked list are comparable. You do that by using Comparable
interface. So your linked list should only contain element that implement Comparable
interface. How do you do that? By giving bound to your type parameter E
:
class MyLinkedList<E extends Comparable<E>> { ... }
This means that, in order to add Car
to your list, it has to implement Comparable<Car>
.