2011-10-28 43 views
0

編輯:繼承人更新的插入代碼。我會盡力斷言,但看我還是犯了一個錯誤鏈表分段錯誤。我猜我在這裏做指針錯誤

bool SortedList::insert(Student *s){  
    bool inList = false;  
    // create an iterator for the list  
    ListNode *current = head;  
    // create a new ListNode for the student to be added  
    ListNode *addition = new ListNode();  
    // initialize addition to the student and the next to NULL  
    addition->student = s;  
    addition->next = NULL;  
      //while current is not at the end of the list  
      while(current != NULL){  
        // if the iteration's ID is equal to the given ID return  
        // false and delete the ListNode addition  
        if(current->student->getID() == addition->student->getID()){  
          delete addition;  
          return false;  
        // else if the next student ID in the list is greater than  
        // the given ID break the while loop  
        }else if(current->next != NULL && current->next->student->getID() > addition->student->getID()){ 
          inList = true;  
          break;  
        }  
        // otherwise set current to the next student in the list  
        current = current->next;     
      }  
    // if current is at the end of the list and student wasn't found, set  
    // current next to addition   
    if(!inList){  
      current->next = addition;  
    // else set addition next to current next next and current next to addition  
    }else{    
      addition->next = current->next;  
      current->next = addition;  
    }   
    // return true regardless as the student has been added  
    return true;  
} 

我已經遇到一些麻煩我一直對這個基本linkedlist.cpp文件。我猜測我可能使用指針錯誤或沿着這些線路。主文件是作爲一個對象文件給出的,所以我不能看它,出於某種原因,我只想測試插入方法(我睡眠非常低並掙扎)。無論如何,我猜在插入方法的地方我引用NULL錯誤,但我不知道在哪裏。

錯誤:段錯誤(核心轉儲) 注:在運行插入

#include <iostream> 
#include "SortedList.h" 

using namespace std; 

/** 
    * zero argument constructor - initializes an empty list 
    */ 
SortedList::SortedList() : head(NULL){} 

/** 
    * If a student with the same ID is not already in the list, inserts 
    * the given student into the list in the appropriate place and returns 
    * true. If there is already a student in the list with the same ID 
    * then the list is not changed and false is returned. 
    * 
    * @param *s a given pointer to a student 
    * @return boolean value based on whether the student was inserted or not 
    */ 
bool SortedList::insert(Student *s){ 
// create an iterator for the list 
ListNode *current = head; 
// create a new ListNode for the student to be added 
ListNode *addition = new ListNode(); 
// initialize addition to the student and the next to NULL 
addition->student = s; 
addition->next = NULL; 
     //while current is not at the end of the list 
     while(current != NULL){ 
       // if the iteration's ID is equal to the given ID return 
       // false and delete the ListNode addition 
       if(current->student->getID() == addition->getID()){ 
         return false; 
         delete addition; 
       // else if the next student ID in the list is greater than 
       // the given ID break the while loop       
       }else if(current->next->student->getID() > addition->getID()){ 
         break;  
       } 
       // otherwise set current to the next student in the list 
       current = current->next;    
     } 
// if current is at the end of the list and student wasn't found, set 
// current next to addition  
if(current == NULL){ 
     current->next = addition; 
// else set addition next to current next next and current next to addition 
}else{   
addition->next = current->next->next; 
current->next = addition; 
}  
// return true regardless as the student has been added 
return true; 
} 

/** 
    * Searches the list for a student with the given student ID. If the 
    * student is found, it is returned; if it is not found, NULL is returned. 
    * 
    * @param studentID the given studentID to find in the list 
    * @return a pointer to a the student found or NULL if the student isn't found 
    */ 
Student * SortedList::find(int studentID){ 
    // create iterator for the list 
    ListNode *current = head; 
    // while not at the end of the list iterate 
    while(current != NULL){ 
      // if the current student ID equals the given student ID return 
      // the student  
      if(current->student->getID() == studentID){ 
        return current->student; 
      } 
      // otherwise continue iterating 
      current = current->next; 
    } 
    // if not found then return NULL 
    return NULL;     
} 

/** 
    * Searches the list for a student with the given student ID. If the 
    * student is found, the student is removed from the list and returned; 
    * if no student is found with the given ID, NULL is returned. 
    * 
    * @param studentID the given student ID to be removed from the list 
    * @return a pointer to the student that was removed or NULL if the student 
    * wasn't found 
    */ 
Student * SortedList::remove(int studentID){ 
    // create iterator for the list 
    ListNode *current = head; 
    // create the to hold the value ahead of the iterator 
    ListNode *currentNext; 
    // create to hold the removed student 
    Student *remove; 
    // while current is not at the end of the list iterate 
    while(current != NULL){ 
        // set currentNext to the value ahead of iterator 
        currentNext = current->next; 
        // if its ID equals the given ID 
        if(currentNext->student->getID() == studentID){ 
         // set remove to the student removing 
         remove = currentNext->student; 
         // set current next to currentNext next 
         // (current next next) 
         current->next = currentNext->next; 
         //delete the removed ListNode 
         delete currentNext; 
         //return the removed student 
         return remove; 
        } 
    } 
    //if the student wasn't found return NULL 
    return NULL; 
} 

/** 
    * Prints out the list of students to standard output. The students are 
    * printed in order of student ID (from smallest to largest), one per line 
    */ 
void SortedList::print() const{ 
    //create iterator for list 
ListNode *current = head; 
      //while current is not at the end of the list iterate 
      while(current != NULL){ 
        // print each individual student and end line  
        current->student->print(); 
        cout << endl; 
        //iterate the list 
        current = current->next;    
      }   
} 
+0

積極使用斷言來幫助縮小問題自己 –

回答

1
while(current != NULL){ 
      // if the iteration's ID is equal to the given ID return 
      // false and delete the ListNode addition 
      if(current->student->getID() == addition->getID()){ 
    2     return false; 
        delete addition; 
      // else if the next student ID in the list is greater than 
      // the given ID break the while loop       
    1   }else if(current->next->student->getID() > addition->getID()){ 
        break;  
      } 

在我打上1行後發生此錯誤,取消引用current->next而沒有檢查是否是NULL或不。另外,在我標記2的那一行,你結束執行,然後然後刪除指針。你應該delete之前return

if(current == NULL){ 
3 current->next = addition; 
// else set addition next to current next next and current next to addition 
}else{   
4  addition->next = current->next->next; 
    current->next = addition; 
} 

在線條爲標誌3,取消引用current只有當它的NULL。壞juju。在標記爲4的行上,您解除引用current->next而不檢查它是否首先是NULL。無論如何,我認爲你的意圖是將addition->next設置爲current->next

+0

我做了所有建議的修復,但仍然無法正常工作。感謝您指出了這些,但是您能否找到其他問題? – chazzwa

+0

@ user1015524:在'pointername->'每行放入assert(pointername)之前;'除此之外,我看不到更新後的代碼,所以我無法評論更新後的代碼。 –

+0

@ user1015524:發現看起來不錯,刪除看起來不錯,除非它不能抹去第一個學生。 –