2014-02-16 121 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

我正在使用标志来检查其指针变量或下一个中的第一个节点。对不起,我没有得到'伯爵'的意思。?你可以再详细一点吗 –