选择排序双链表java

选择排序双链表java,java,sorting,linked-list,Java,Sorting,Linked List,我需要使用选择排序对链表进行排序。 但我不能使用集合。 我在查找最小元素和创建新版本的排序列表时遇到了麻烦。 谢谢 由于非工作findsmalestmethodSort方法无法正常工作。我试图通过创建一个新的列表来实现排序,在列表中我以排序的方式放置节点。而且它也不会超出“While循环” 它发送添加的最大元素,如果我手动将“最后一个”指定给“最小的”,它就会工作 public Node findSmallest(Node toStartFrom) { N

我需要使用选择排序对链表进行排序。 但我不能使用集合。 我在查找最小元素和创建新版本的排序列表时遇到了麻烦。 谢谢

由于非工作
findsmalest
method
Sort
方法无法正常工作。我试图通过创建一个新的列表来实现排序,在列表中我以排序的方式放置节点。而且它也不会超出“While循环”

它发送添加的最大元素,如果我手动将“最后一个”指定给“最小的”,它就会工作

        public Node findSmallest(Node toStartFrom) {
            Node current = toStartFrom;
            Node smallest = toStartFrom; //if i put here `last` it will work correctly
            while(current != null) {
                if (smallest.student.name.compareToIgnoreCase(current.student.name) > 0) smallest = current;
                current = current.previous;
            }
            return smallest;
        }

    }

  public class Node {
        public Student student;

        public Node next;
        public Node previous;

        public Node(Student student) {
            this.student = student;
        }
    }


    public class Student {
        public String name;
        public String surname;
        public String educationType;

        static public Student createStudent() {
         ....
            return student;
        }
    }

不使用双链接列表可能会有所帮助,因为这样您需要维护的链接就更少了。此外,FindSalest()方法可能会遇到问题,因为您最初将自己设置为同一节点的当前和最小值,因此在执行if(minimest.student.name.compareTignoreCase(current.student.name)>0)语句时,您会将学生的名称与学生的相同名称进行比较。例如,如果设置为最小的节点的学生名为John well current,则将该节点设置为相同的节点,因此current的学生名也是John。如果他们是同名的不同学生,但在代码中他们是相同的学生,并且当前和最小点都指向同一节点,则这不是问题。实际上,这个if语句总是错误的,并且您永远不会执行代码在列表中移动当前值。这也是为什么当您设置minimest=last时,该方法至少在某些时候有效

如上所述,尝试以下操作

最小=起始节点
下一步=开始节点。下一步
while(next!=null)
将“下一个”与“最小”进行比较,并相应地分配
下一步=下一步。下一步

将其转换为代码应该不太难

定义“不工作”。另外,如果这是一个作业,它应该被标记为家庭作业。是的,以什么方式“不工作”?NPE不工作或随机返回不工作或什么?另外,有没有任何原因表明您正在以相反的顺序遍历列表?我觉得下一个是大会
        public void Sort() {
            LinkedList list = new LinkedList();
            Node toStart = last;
            while (toStart!=null){
                list.addLast(findSmallest(toStart).student);
                toStart = toStart.previous;
            }


        }
        public Node findSmallest(Node toStartFrom) {
            Node current = toStartFrom;
            Node smallest = toStartFrom; //if i put here `last` it will work correctly
            while(current != null) {
                if (smallest.student.name.compareToIgnoreCase(current.student.name) > 0) smallest = current;
                current = current.previous;
            }
            return smallest;
        }

    }

  public class Node {
        public Student student;

        public Node next;
        public Node previous;

        public Node(Student student) {
            this.student = student;
        }
    }


    public class Student {
        public String name;
        public String surname;
        public String educationType;

        static public Student createStudent() {
         ....
            return student;
        }
    }