2014-02-16 94 views
0

您好我收到空指針異常從排序方法中的此代碼無法找出原因。我會很感激任何迴應。 它在行號碼93在排序方法下迴路next = first.next。 由於提前排序元素鏈接列表插入排序Java

public class LinkedList { 
    Student first,last,match,pointer; 
    Course start,end; 
    int linkCount = 1; 
    public LinkedList(){ 
     first = null; 
     last = null; 
    } 
    public boolean isEmpty(){ 
     return first==null; 
    } 
    public void deleteLink(int id){ 
     Student firstnext = first; 
     if(id==first.id){ 
      Student temp = first.next; 
      first = temp; 
     } 
     else if(last.id==id){ 
      while(first!=null){ 
       if(first.next.id == id){ 
        first.next=null; 
       } 
       first = first.next; 
      } 
      first = firstnext; 
     } 
     else{ 
      while(first.next!=null){ 
        if(first.next.id == id){ 
         first.next = first.next.next; 
        }     
        first = first.next; 
       } 
      first = firstnext; 
     } 

    }  
    public void traverseStudent(){ 
     Student currentStudent = pointer;  
     while(currentStudent != null) { 
      currentStudent.printLink(); 
      currentStudent.traverseCourse();      
      currentStudent = currentStudent.next;      
     } 
     System.out.println(""); 
    }  
    public void insert(String fname, String lname, int id, String courseId, int credits,char grade){ 
     if(isExist(id)){ 
      match.insertCourse(courseId,credits,grade); 
     } 
     else{ 
      Student link = new Student(fname, lname, id); 
      if(first==null){ 
       link.next = null; 
       first = link; 
       last = link; 
      } 
      else{ 
       last.next=link; 
       link.next=null; 
       last=link; 
      } 
      linkCount++; 
      link.insertCourse(courseId,credits,grade);    
     }   
    } 
    public void sort(){ 
     Student current,next,firstLink = first,temp=null; 
     int flag = 0,flag2 =0; 
     pointer = null; 

     if(first!=null){ 
      if(first.next==null){ 
       current = first; 
      } 
      else{ 
       while(linkCount>0){      
        current = first; 
        next = first.next; 

        while(next!=null){ 
         if(current.lName.compareToIgnoreCase(next.lName)>0){ 
          current = next; 
          if(flag2 == 0) 
           flag = 1; 
         }     
         next = next.next;     
        } 
        first = firstLink; 
        if(flag == 1){ 
         deleteLink(current.id); 
         current.next = null; 
         pointer = current; 
         temp = pointer; 
         flag =0; 
         flag2 =1; 
        } 
        else if(flag2 ==1){ 
         deleteLink(current.id); 
         current.next = null; 
         pointer.next = current; 
         pointer = pointer.next; 
        }      
       linkCount--;      
       }     
      } 
      pointer = temp; 
     } 
    } 

     public boolean isExist(int id){ 
      Student currentStudent = first;   

      while(currentStudent != null) { 
       if(currentStudent.id==id){ 
        match = currentStudent; 
        return true; 
      } 
     currentStudent = currentStudent.next; 
     }    
     return false; 
    } 
} 
+1

你介意和我們這行究竟有NPE共享。 –

+0

在while循環「next = first.next」下的Sort方法中,它的行號爲93。 –

回答

0

,當你調用一個空值的方法會出現此錯誤。該方法無法運行,因爲給定的參數沒有值。

由於您沒有提供導致問題的特定行代碼,因此我只能說在sort()之前檢查您使用的所有變量,並確保在調用它們之前對其進行了初始化。

+0

我試了一切,如果你看到代碼,你會發現我正在對空變量進行適當的檢查。 –

+0

哪一行具體有錯誤? –

+0

在while循環「next = first.next」下的Sort方法中,它的行號93。 –

0

選擇/插入排序應該有兩個循環(Outer和Inner)=> O(n^2)。當指向當前值的指針大於當前正在評估的值時 - 應交換節點。

僞代碼:

Sort = function(first) { 
    var current = first; 
    while(current!= null) { 
    var innerCurrent = current.next; 

     while(innerCurrent != null) { 
      if(innerCurrent.Value < current.Value) { 
       Swap(current, innerCurrent); 
      } 
      innerCurrent = innerCurrent.next; 
     } 

     current = current.next; 
    } 
} 

Swap = function(current, innerCurrent) { 

    var temp; 
    temp.Value = current.Value; 
    temp.Next = current.Next; 
    temp.Prev = current.Prev; 

    current.Value = innerCurrent.Value; 
    current.Next = innerCurrent.Next; 
    current.Prev = innerCurrent.Prev; 

    innerCurrent.Value = temp.Value; 
    innerCurrent.Next = temp.Next; 
    innerCurrent.Prev = temp.Prev; 

} 
+0

嗨,感謝您的回覆,實際上我使用了兩個你會在閱讀時看到的循環。並以我刪除節點的方式,以便外層循環不計算已刪除的元素。 –

+0

刪除節點不應該「計數」它 - 因爲在交換之後,您將轉到列表中的下一個節點。我想這個代碼可以簡化。爲什麼在交換中使用標誌? – Newse

+0

我正在使用標誌來檢查其指針變量或下一個中的第一個節點。對不起,我沒有得到'伯爵'的意思。?你可以再詳細一點嗎 –