2011-10-28 8 views
0

編集:更新された挿入コードをHeres。私はそれを主張してみてください、私はまだ、私は私が取り組んできた、この基本的なlinkedlist.cppファイルといくつかの問題を抱えてきた過ちリンクリストセグメンテーションフォルト。私はここでポインタの何かが間違っていると推測している

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

を作ってるんだかどうかを確認します。私はおそらくポインタが間違っているか、それらの行に沿って何かを使用していると思います。メインファイルはオブジェクトファイルとして与えられているので、私はそれを見ることができません。何らかの理由で私は挿入メソッドをテストすることしか考えていませんでした(私は睡眠と苦労では非常に低いです)。とにかく私はどこかに挿入メソッドで推測している私は間違ってNULLを参照していますが、私はどこを把握することはできません。

ERROR:セグメンテーションフォールト(コアダンプ) 注:このエラーは、それがNULLかであれば、これまで確認することなく、あなたがcurrent->next逆参照、私は1をマークラインで

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

の挿入を実行した後に発生しましたない。また、2行目でexectutionを終了し、、次にポインタを削除します。 returnの前にdeleteにする必要があります。ラインで

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

あなたはそれがNULLだ場合にのみcurrent逆参照、3をマーク。悪いジュジュ。 4と記された行で、NULLが最初にあるかどうかを確認することなく、current->nextを逆参照します。とにかくaddition->nextcurrent->nextに設定すると思います。

+0

私はすべての推奨修正を行いましたが、それでも機能しません。それらを指摘していただきありがとうございますが、それ以外の何かを見つけることができますか? – chazzwa

+0

@ user1015524: 'pointername->'が 'assert(pointername);を入れたすべての行の前に、'それ以外は更新されたコードを見ることができないので、更新されたコードについてはコメントできません。 –

+0

@ user1015524:最初の生徒を消去できないことを除いて、見栄えがよく見えます。 –

関連する問題