排序元素链接列表插入在Java中排序

时间:2014-02-16 00:51:38

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

您好我从排序方法中的此代码获取Null指针异常无法找出原因。我很感激任何回应。 它位于 循环下一个= first.next 下的Sort方法中的第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;
    }
}

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

}