选择排序d链表



我在双链表中实现选择排序。我要按姓氏排序找到最小的元素并把它插入到列表的开头。但也有一些麻烦,当我运行我的程序,我有NIL异常在排序方法在while循环。它是整个应用,你可以编译并运行。帮助将会很感激。谢谢。

    public class LinkedList {
        public Node first;
        public Node last;
        public LinkedList() {
            first = null;
            last = null;
        }
        public void addFirst(Student student) {
            Node f = first;
            Node newNode = new Node(student);
            first = newNode;
            if (f == null) last = newNode;
            else {
                f.previous = newNode;
                newNode.next = f;
            }
        }
        public void addLast(Student student) {
            Node l = last;
            Node newNode = new Node(student);
            last = newNode;
            if (l == null) first = newNode;
            else {
                l.next = newNode;
                newNode.previous = l;
            }
        }
        public void display() {
            Node current = first;
            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.next;
            }
        }
        public Node findSmallest(Node startNode) {
            Node smallest = startNode;
            while (startNode.next != null) {
                if (smallest.student.surname.compareToIgnoreCase(startNode.next.student.surname) > 1)
                    smallest = startNode.next;
                else startNode = startNode.next;
            }
            return smallest;
        }
        public void Sort() {
            LinkedList newList = new LinkedList();
            Node current = first;
            while (current.next != null) {
                Node smallest = findSmallest(current);
                newList.addLast(smallest.student);
                delNode(smallest);
                current = current.next;
            }
            first = newList.first;
            last = newList.last;
        }
        public void delNode(Node toDel) {
            if (toDel.previous == null) {
                toDel.next.previous = null;
                first = toDel.next;
                return;
            }
            if (toDel.next == null) {
                toDel.previous.next = null;
                last = toDel.previous;
                return;
            }
            toDel.previous.next = toDel.next;
            toDel.next.previous = toDel.previous;
        }
}

public class Student {
    public String name;
    public String surname;
    public String educationType;
    static public Student createStudent() {
        Student student = new Student();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Enter student's name:");
            student.name = br.readLine();
            System.out.println("Enter surname:");
            student.surname = br.readLine();
            System.out.println("Enter educational type:");
            student.educationType = br.readLine();
        } catch (IOException e) {
            throw new NotImplementedException();
        }
        return student;
    }
}

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

我没有运行它,所以它只是从查看您的代码:

  1. 这不是真正的选择排序,因为您正在构建新列表而不是对旧列表进行排序。

  2. delNode()将失败与NPE单元素列表,所以也许这就是问题所在(它也会失败,如果你删除最后一个元素)。

相关内容

  • 没有找到相关文章

最新更新