Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: organizing linkedlist in ascending order

  1. #1
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default 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)

    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;            
            }            
        }
     
    }

    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();        
     
     
        }
    }


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,659
    Thanks
    25
    Thanked 325 Times in 302 Posts

    Default Re: organizing linkedlist in ascending order

    I assume your Student object implements the Comparable object. How did you write your compareTo() method?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: organizing linkedlist in ascending order

    Quote Originally Posted by aussiemcgr View Post
    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

    //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

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,659
    Thanks
    25
    Thanked 325 Times in 302 Posts

    Default 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.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: organizing linkedlist in ascending order

    Quote Originally Posted by aussiemcgr View Post
    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

    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

    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

  6. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,659
    Thanks
    25
    Thanked 325 Times in 302 Posts

    Default 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);
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  7. #7
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: organizing linkedlist in ascending order

    you mean that I should have "Comparable" class inside "Student" class that implements a compareTo() method?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,414
    Thanks
    49
    Thanked 2,214 Times in 2,187 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: October 22nd, 2011, 01:53 AM
  2. Issues with ascending number program
    By newtojava2011 in forum What's Wrong With My Code?
    Replies: 21
    Last Post: June 30th, 2011, 06:23 PM
  3. Replies: 3
    Last Post: June 1st, 2011, 12:47 AM
  4. Threads in some order...
    By aps135 in forum Threads
    Replies: 6
    Last Post: March 11th, 2011, 04:54 PM
  5. Replies: 4
    Last Post: November 14th, 2010, 10:44 AM