2009-05-28 18 views
1

Problem1:7のリストにおける第6ノードのC++キュー内のノードを削除

削除、最初 の印刷で結果:> 3

記述リスト内のノードに削除最後のノード。

使用可能なノードのポインタ:DeleteNode: * NEXT_、* prev_、*指定されたノードを削除するDATA_

機能はLinkedList.cpp 名前です。ノードを印刷するには、リストをトラバース

機能はmain.cppに 名前である:ノードを印刷するようにトラバースするとき

メインに電流 - > prev_アクセスできること:

考えられる解決策はPrintAllNodes 。

コード:

void LinkedList::DeleteNode(Node* node) 
{ 
    Node *Current = first_; // I want this to be my only Node Ptr Varaible Declaration. 
    if (NULL == first_) 
     std::cout << "Cannot delete from an empty list: \n"; 
//TRAVERSING WAS/IS A BAD IDEA............................. 
    while (Current != NULL) 
    { 
     if (Current->data_ == node->data_) 
     { 
      //If Current isn't the head of the list, set prev to next 
      if (Current != first_) 
      { 
       Current->prev_  = first_; //statement that follows crashes if this is not assigned. 
       Current->prev_->next_ = Current->next_; 
      } 
      else 
      { 
       first_ = Current->next_; 
       if (first_ != NULL) 
       first_->prev_ = NULL; 
      } 

      //If Current isn't the tail of the list, set next to prev 
      if (Current->next_ != NULL) 
       Current->next_ = Current->prev_; 

      else if (Current->prev_ != NULL) 
      Current->prev_->next_ = NULL; 

      listLen_--; 
      delete Current; 
      Current = NULL; 
     } 
     else 
     { 
      Current->prev_ = Current; 
      Current = Current->next_; 
     } 
    } 
    return; 
} 

コードmain.cppにでPrintAllNodes用:

void PrintAllNodes(LinkedList *LinkedObject, long length = 0) 
{ 
    const char *Names = NULL; 
    length = LinkedObject->GetListLength(); 
    Node *GetNode = LinkedObject->GetFirstNode(); 

    for (signed short x = 0; x < length; x++) 
    { 
     Names = static_cast< NameObject* >(GetNode->data_)->GetName(); 
     cout << Names << endl; 
     GetNode = GetNode->next_; // traversing 
    } 
    return; 
} 
+0

質問?宿題の質問を実際に言い換えることができ、コピー/ペーストの代わりに問題と思われるものを指摘し、他の人が解決するのを待つならば、あなたの質問に価値を加えることができます。 – stefanB

答えて

2

これはあなたの問題である:

Current->prev_ = first_; 

前にすべてのノードを切断されたあなたがやっています現在と最後のものをつなぐ! (あなたのケースでは七)

あなたは何をすべき唯一の存在である。

Current->prev_->next_ = Current->next_; 
Current->next_->prev_ = Current->prev_; //I think you forgot this 
delete Current; 
Current = NULL; 

あなたがクラッシュを取得

Current->prev_ = first_; 

のない場合は、それがあなたの電流 - > prev_ ISN」ためです割り当てられている。 しかし、それを最初に割り当てることは解決策ではありません。 他の方法(おそらくAddNode)を調べてCurrent-> prev_が悪い理由を調べるべきです。

関連する問題