繁体   English   中英

选择排序双链表Java

[英]Selection sort Doubly linked list java

我需要使用选择排序对链接列表进行排序。 但是我不能使用收藏集。 我很难找到最小的元素并创建新版本的排序列表。 谢谢。

    public class LinkedList {
        public Node first;
        public Node last;

        public LinkedList() {
            first = null;
            last = null;
        }

        public boolean isEmpty() {
            return first == null;
        }

        public void addFirst(Student student) {
            Node newNode = new Node(student);
            if (isEmpty())
                last = newNode;
            else
                first.previous = newNode;
            newNode.next = first;
            first = newNode;
        }

        public void addLast(Student student) {
            Node newNode = new Node(student);
            if (isEmpty())
                first = newNode;
            else
                last.next = newNode;
            newNode.previous = last;
            last = newNode;
        }

        public void display() {
            Node current = last;
            while (current != null) {
                System.out.print(current.student.name + "\b");
                System.out.print(current.student.surname + "\b");
                System.out.println(current.student.educationType);
                current = current.previous;
            }
        }

由于findSmallest方法无法正常工作, findSmallest Sort方法无法正常工作。 我试图通过创建一个新列表来实现排序,在该列表中我将Nodes进行排序。 而且它也不会超出“ While loop”

        public void Sort() {
            LinkedList list = new LinkedList();
            Node toStart = last;
            while (toStart!=null){
                list.addLast(findSmallest(toStart).student);
                toStart = toStart.previous;
            }


        }

它发送添加的最大元素,如果我手动将“ last”分配给“ smallest”,它将起作用。

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

没有双向链接列表可能会有所帮助,因为这样一来,您需要维护的链接就会减少。 另外,您可能在findSmallest()方法上遇到麻烦,因为您最初将当前和最小设置为同一节点,因此当执行if(smallest.student.name.compareToIgnoreCase(current.student.name)> 0)语句时您正在将学生的姓名与学生的姓名进行比较。 例如,如果将最小的节点设置为具有John的学生名,那么current当前被设置为同一节点,因此current的学生名也是John。 如果他们是同名的不同学生,但在您的代码中他们是同一学生,并且当前和最小指向同一节点,这不是问题。 实际上,此if语句始终会为false,并且您将永远不会执行代码以使电流沿列表移动。 这就是为什么当您设置minimum = last时,该方法至少在某些时候有效。

如上所述,尝试类似

smallest = startnode
next =startnode.next
while(next != null)
compare next with smallest, and assign accordingly
next = next.next

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

暂无
暂无

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

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