2011-10-29 18 views
1

選択ソートを二重リンクリストに実装しています。 私は最小の要素を見つけてそれをリストの先頭に挿入することで、姓によってリストを並べ替える必要があります。 しかし、いくつかの問題があります。プログラムを実行すると、whileループでSortメソッドで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. 新しいリストを構築する代わりに、古いものを仕分けしているとしてそれは、本当に選択ソートではありません場所。

  2. 単一要素リストの場合、delNode()はNPEで失敗するため、間違っている可能性があります(最後の要素を削除すると失敗します)。

  3. あなたが許可されていない限り、看板を使用してください。