【问题标题】:Sorting Elements Linked List Insertion Sort in JavaJava中的元素排序链表插入排序
【发布时间】:2014-03-15 09:03:37
【问题描述】:

您好,我在排序方法中从这段代码中得到空指针异常,无法弄清楚原因。我将不胜感激。 它位于 while 循环 next= first.next 下的 Sort 方法中的 line number 93。 提前致谢

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;
    }
}

【问题讨论】:

  • 您是否介意与我们分享哪条线路具有 NPE。
  • 它在while循环“next= first.next”下的Sort方法中的第93行。

标签: java sorting linked-list singly-linked-list insertion-sort


【解决方案1】:

当您调用具有空值的方法时会发生此错误。该方法无法运行,因为给定的参数没有值。

由于您没有提供导致问题的特定代码行,我只能说检查您在sort() 中使用的所有变量,并确保它们在调用之前已初始化。

【讨论】:

  • 我尝试了一切,如果您看到代码,您会发现我正在对空变量进行适当的检查。
  • 它在while循环“next= first.next”下的Sort方法中的第93行。
【解决方案2】:

选择/插入排序应该有两个循环(外部和内部)=> 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;

}

【讨论】:

  • 您好,感谢您的回复,实际上我正在使用两个循环,您会在阅读时看到。并且以我删除节点的方式,以便外部循环不计算已删除的元素。
  • 删除节点不应该“计算”它 - 因为在交换之后您将转到列表中的下一个节点。我认为这段代码可以简化。你为什么在交换中使用标志?
  • 我正在使用标志来检查它是指针变量中的第一个节点还是下一个节点。对不起,我没明白你所说的“伯爵”是什么意思。?你能说得更具体些吗
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-13
  • 2011-04-23
  • 1970-01-01
相关资源
最近更新 更多