organizing linkedlist in ascending order
guys, I have the following method (add), and as I'm adding values to the list I need to order in ascending order based on firstNmae+" "+lastName. I tried using the Collections.sort(students) in the main method, but that doesn't work, besides I need to come up with an implementation that compares new data name to the rest of list names( I think that is the right algorithm)
Code :
public class LinkedList {
private Node first;
public LinkedList()
{
this.first = null;
}
//add students to the list
public void add(Student s)
{
Node newNode = new Node(s);
newNode.next = first;
first = newNode;
}
//remove duplicate records (return true if duplicate found)
// public boolean remove(String fn, String ln)
// {
//
// }
//display list of student
public void display()
{
if(first == null)
System.out.println("List is empty!");
else
{
System.out.println(first.value);
first = first.next;
}
}
}
Code :
public class Tester {
public static void main(String[] args) {
UnderGrad john = new UnderGrad("john", "doe", 2.7, "computer Science", "phisics");
UnderGrad jorge = new UnderGrad("jorge", "vazquez", 3.8, "computer Science", "programming");
Advisor jim = new Advisor("jim", "smith");
Grad jane = new Grad("jane", "doe", 3.0, "Electric Engineering", jim);
LinkedList students = new LinkedList();
students.add(john);
students.add(jorge);
students.add(jane);
students.display();
}
}
Re: organizing linkedlist in ascending order
I assume your Student object implements the Comparable object. How did you write your compareTo() method?
Re: organizing linkedlist in ascending order
Quote:
Originally Posted by
aussiemcgr
I assume your Student object implements the Comparable object. How did you write your compareTo() method?
forgot to mentioned it... it is implemented in the Person class
Code :
//method to compare student names
public int compareTo(String fn, String ln)
{
return this.toString().compareTo(fn+" "+ln);
}
how could I use this method to compare in the "add" method of the Linkedlist class?
Thanks
Re: organizing linkedlist in ascending order
Go through the LinkedList, node by node. If node.next is greater than the new node you created, new node's next is equal to node.next and node's next is equal to the new node.
Re: organizing linkedlist in ascending order
Quote:
Originally Posted by
aussiemcgr
Go through the LinkedList, node by node. If node.next is greater than the new node you created, new node's next is equal to node.next and node's next is equal to the new node.
the only way I can use the "compareTo" method I implemented in the student class is if I use like this
Code :
public void add(Student s)
{
Node newNode = new Node(s);
newNode.next = first;
first = newNode;
while(first != null)
{
if(first.value.compareTo(s.getFname(), s.getLname()) > 0)
{
}
}
}
and in this case I'm comparing the fName, lName to the first node that I just created; therefore, they are equal.
the other way of doing it is by comparing the whole toString() which include major gpa and all that stuff
Code :
if(first.next.toString().compareTo(newNode.toString()) > 0)
the second would do the comparison, but still the to string contains all other info beside just the firstname and lastname, and I don't think that is the right way, so I'm kind of confuse
Re: organizing linkedlist in ascending order
Earlier I had mentioned implementing the Comparable interface in the Student class. The reason I said that was because Collections.sort() requires you to send it a list of objects that implements the Comparable interface. If you implement Comparable in the Student class and implement the required compareTo() method, you can just use: Collections.sort(students);
Re: organizing linkedlist in ascending order
you mean that I should have "Comparable" class inside "Student" class that implements a compareTo() method?
Re: organizing linkedlist in ascending order
Your Student class should implement the Comparable interface, not have a class inside of it.
Implementing an interface requires a class to define all the methods that the interface has. The Student class would define all the methods in the Comparable interface.