简体   繁体   English

按升序排序双链表

[英]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. 我需要按照ID的升序对包含其ID,姓名,年龄和GPA的学生列表进行排序。 My method insertAscending is attempting to do that but for some reason won't work properly. 我的方法insertAscending正在尝试执行此操作,但由于某种原因无法正常工作。 my private inner Node class is included too. 我的私有内部Node类也包括在内。 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. 将LinkedList保持在已排序的顺序中会否定LinkedList的很多好处,LinkedList的头和尾是O(1)插入。 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. 通常,对我来说,这听起来像是TreeMap的工作,其中键是学生ID,值是Student对象。 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阶梯中添加新尾巴的部分应如下所示:

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. 以前,您没有更新旧尾巴的next指针。 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. 你没有设置prev老脸因为在新的头部添加的指针。

Other quick thoughts: 其他快速的想法:

  • You should explicitly init size to 0 in your constructor 您应该在构造函数中将init size显式初始化为0

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: 然后,我添加了一个简单的main方法,如下所示:

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. 一旦确定单元素案例有效,就取消了对下一个insertAscending调用的注释,直到所有步骤都奏效为止。

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. 我认为真正重要的是,您了解我是如何发现这些错误的,并且您想出了一种更好的方法来测试您的代码并说服它正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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