2011-10-29 53 views
1

我在雙向鏈表中實現選擇排序。 我必須通過找到最小的元素並將其插入列表的開頭來按姓氏排序。 但是有一些麻煩,當我運行我的程序時,我在循環中的排序方法中有NIL異常。 這是整個應用程序,所以你可以編譯它並運行,如果你想。 幫助將不勝感激。 謝謝。選擇排序DLinked列表

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

我沒有運行它,所以它從看你的代碼只是:

  1. 這不是一個真正的選擇排序,因爲你是製造新的列表,而不是排序老1地點。

  2. delNode()將失敗,NPE單元素列表,所以也許這是出了什麼問題(如果你刪除最後一個元素,它也將失敗)。

  3. 除非你不準,使用哨兵。