简体   繁体   中英

sorting doubly linked lists by ascending order

I need to sort a student list containing their IDs, names, ages and GPAs according to ascending order of their IDs. My method insertAscending is attempting to do that but for some reason won't work properly. my private inner Node class is included too. any help would be greatly appreciated.

public class StudentInfo
{
    //Node class (inner class)
    private class Node
    {
        private int id;
        private String name;
        private int age;
        private double gpa;
        private Node next;
        private Node prev;


        //constructor of Node
        private Node(int id, String name, int age, double gpa, Node prev, Node next)
        {
            this.id = id;
            this.name = name;
            this.age = age;
            this.gpa = gpa;
            this.prev = prev;
            this.next = next;

        }
    }
   /*******************************************************************/

   private Node head;
   private Node tail;
   int size;
   //constructor of list
   public StudentInfo()
   {
       head = null;
       tail = null;
   }
   /*********************************************************************/

   public void insertAscending(int id, String name, int age, double gpa)
   {



     if (head == null)
     { 
         head = tail = new Node(id, name, age, gpa, null, null );
         size ++;

     }

     //insert at beginning
     else if( id < head.id)
     {

         head = new Node(id, name, age, gpa, null, head);
         size ++;

     }


     //insert at end
     else if(id > tail.id)
     {
         tail = new Node(id, name, age, gpa, tail, null);
         size++;
     }

     //insert at other positions 
     else 
     {
         for(Node temp = head; temp.next != null; temp = temp.next)
         {
             if (id > temp.id && id < temp.next.id)
             {
                 temp.next = new Node(id, name, age, gpa, temp, temp.next);
                 temp.next.next.prev = temp.next;
                 size++;
                 break;
             }
         }
     }

   }
}

Is this part of an assignment? Do you have to use a Doubly Linked List for this?

Keeping a LinkedList in sorted ordered negates a lot of the benefit of a LinkedList which is the O(1) insertion at head and tail. Normally this would sound to me like a job for a TreeMap where the key is the student ID and the value is the Student object. That would give you iteration in student ID order plus much faster lookup.

As to your actual issues with this code, I think you're forgetting to update some of the heads/tails of the nodes that are already in the list (not the ones you're inserting). For instance, I think your section of your else/if ladder to add a new tail should look something like this:

else if(id > tail.id)
{
     Node newTail = new Node(id, name, age, gpa, tail, null);
     tail.next = newTail;
     tail = newTail;
     size++;
}

Previously you didn't update the next pointer of the old tail. Likewise I think your code for setting a new head should look something like this:

else if( id < head.id)
{
     Node newHead = new Node(id, name, age, gpa, null, head);
     head.prev = newHead;
     head = newHead;
     size ++;
}

You weren't setting the prev pointer of the old head because adding in the new head.

Other quick thoughts:

  • You should explicitly init size to 0 in your constructor

More than correcting any other errors though, I think the bigger question here is: how are you testing this? I started finding some of these errors pretty easily just by running some quick tests. I added a couple convenience methods to the class (just for testing...there are better ways to do iterators):

public void print()
{
   for(Node temp = head; temp != null; temp = temp.next)
   {
       System.out.println(temp.name);
   }
}

public void printReverse()
{
   for(Node temp = tail; temp != null; temp = temp.prev)
   {
       System.out.println(temp.name);
   }
}

And then I added a simple main method that looked like this:

public static void main(final String[] args)
{
   StudentInfo si = new StudentInfo();
   si.insertAscending(2, "Brent", 23, 2.2);
   //si.insertAscending(3, "Steve", 23, 2.2);
   //si.insertAscending(5, "Joe", 23, 2.2);
   //si.insertAscending(4, "Pat", 23, 2.2);
   //si.insertAscending(1, "Mike", 23, 2.2);
   si.print();
   System.out.println("==========");
   si.printReverse();
}

Even without using a debugger or anything, that made it pretty easy to see what was going wrong. Once I was sure the one-element case worked, I uncommented the next insertAscending call and so-on until it all worked.

There may or may not be other errors in the code that I haven't mentioned here. I think the really important thing is that you understand how I found these bugs and you come up with a better way to test out your code and convince yourself it works properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM