2016-05-06 8 views
0
template<class theType> 
newStudent<theType>* StudentList<theType>::getLocationOfID(const int & target, 
                  newStudent<theType> * theNext) 
{ 
    newStudent<theType> * result = NULL; 

    if(theNext != NULL) 
    { 
     if (theNext->getID() == target) 
      result = theNext; 
     else 
      result = getLocationOfID(target, theNext->getNext()); 
    } 

    else 
     return result; 

} // end recursive getLocationOfID 


template<class theType> 
bool StudentList<theType>::RemoveAStudent() 
{ 

    newStudent<theType> * tmp = head, * prev = head, * curr = head; 

    bool ableToRemove = true; 

    int IDtoRemove; 
    char answer; 

    //no nodes 
    if(isEmpty()) 
    { 
     cout << "\n\t\t   No student transcript stored yet" 
      << "\n\t\t  Please add the student list in order to remove" << endl; 
     return !ableToRemove; 
    } 
    else 
    { 
     cout << "\n\t\tWhich student ID would you like " 
      << "to remove from the list? "; 
     cin >> IDtoRemove; 

     //remove the student located in the head 
     if(head->getID() == IDtoRemove) 
     { 
      counter--; 
      newStudent<theType> * tmp = head; 
      head = getLocationOfID(IDtoRemove, head); 
      delete tmp; //deletes in the head (first Node) 

      cout << "\n\t\tThe Student Transcript belonging to ID " 
       << IDtoRemove << " has been removed."; 
      return ableToRemove; 
     } 

     else 
     { 
      newStudent<theType> * tmp = head, * prev = head, * curr = head; 

      while(curr->getNext() != NULL && curr->getID() != IDtoRemove) 
      { 
       prev = curr; 
       curr = getLocationOfID(IDtoRemove, curr); 
      } 

      //removes in the middle 
      if(curr->getNext() != NULL) 
      { 
       counter--; 
       prev->setNext(curr->getNext()); 
       delete(curr); //delete anywhere in the middle 

       cout << "\n\t\tThe Student Transcript belonging to ID " 
        << IDtoRemove << " has been removed."; 
       return ableToRemove; 
      } 

      else 
      { 
       //remove at the end 
       if(curr->getID() == IDtoRemove) 
       { 
        counter--; 
        prev->setNext(NULL); 
        delete(curr); //removes last node 

        cout << "\n\t\tThe Student Transcript belonging to ID " 
         << IDtoRemove << " has been removed."; 
        return ableToRemove; 
       } 

       else 
        cout << "\n\t\tThe ID " << IDtoRemove << " is not in the list.\n"; 
      } 
     } 
    } 
} 

リストからノードを削除しようとすると、削除するたびにクラッシュし、関数printを呼び出して残りのノードをリストに表示します。RemoveAStud()の問題を解決してノードとクラッシュを削除しないでください

私はリストとクラッシュから私はそれを削除するたびにノードを削除し、リスト内の残りのノードを印刷する機能のプリントを呼び出そうと

をこれを理解助けてください。

私はそれが唯一の問題だが、私は薄いそれがクラッシュを説明できる問題だ場合、私は知らない

答えて

0

をこれを理解助けてください。コード

 while(curr->getNext() != NULL && curr->getID() != IDtoRemove) 
     { 
      prev = curr; 
      curr = getLocationOfID(IDtoRemove, curr); 
     } 

getLocationOfID()のこの部分で

は(私は間違っていないよ場合)IDtoRemoveがリストに存在しないNULL IDにcurrを設定します。この場合には、curr->getNext()はNULLポインタデリファレンス--->クラッシュにしてみてください。

あなたは何をしたいことは

 for (curr = head ; curr && (curr->getID() != IDtoRemove) ; curr = curr->getNext()) 
     { 
      prev = curr; 
     } 

ないことを確認していますか?

ただし、逆参照する前にcurrNULLであるかどうかを確認するには、(次へ)をチェックしてください。

p.s .:申し訳ありませんが、私の悪い英語です。

--- EDIT ---

私はRemoveAStudent()getLocationOfID()の使用はunusefullと危険であると思われます。削除するノードがヘッド(if)であることを知ったとき、あなたがそれを使用

他の点は

if(head->getID() == IDtoRemove) 
    { 
     counter--; 
     newStudent<theType> * tmp = head; 
     head = getLocationOfID(IDtoRemove, head); 
     delete tmp; //deletes in the head (first Node) 

です。そう

 head = getLocationOfID(IDtoRemove, head); 

 head = head; 

のようであるあなたの意図は

 head = head->getNext(); 

でしたか?

そうしないと、headtmp == head)を削除し、次のリストを失います。

関連する問題