簡體   English   中英

排序元素鏈表插入Java排序

[英]Sorting Elements Linked List Insertion Sort in Java

嗨,我從排序方法中的此代碼中獲取Null Pointer Exception無法弄清原因。 我將不勝感激。 它在while循環next = 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;
    }
}

當您調用具有空值的方法時,會發生此錯誤。 該方法無法運行,因為給定的參數沒有值。

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

選擇/插入排序應具有兩個循環(外部和內部)=> 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;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM