2016-10-03 6 views
0

C++で二重リンクリストを実装しようとしていますが、add関数が正しく機能していますが、findノード関数がリストを変更しています。 insertAfter、deleteなどの他の関数はすべてこのfind関数に依存するため、期待通りに動作しません。リンクリスト検索機能の変更リスト

私はC++を初めて使っているので、ポインタを完全に理解していません。私はJavaプログラムをC++で複製しようとしました。 find関数では、ヘッドノードへのポインタが問題の原因になっていることを知っていますが、どうやってそれをどうやって理解するのかはわかりません。以下は

私のコードです:

struct Node{ 
int data; 
Node* next; 
Node* prev; 


Node(int d) { 
    data = d; 
    }; 
}; 


struct DLL { 
    Node* head; 
    Node* tail; 
    int size; 

//Adding a Node to the Doubly LL 
void addNode(Node* n) { 
    //If LL is empty add the first Node 
    if (tail == NULL) { 
     tail = n; 
     head = n; 
    } 
    //Else add add node to the tail. Connect n to the tails next and make n the tail 
    else { 
     tail->next = n; 
     n->prev = tail; 
     tail = n; 
     tail->next = NULL; 
    } 

    size++; 
}; 

//Finding a random Node in the linked List 
//It will return the Node with the FIRST occurrence where data = d 
Node* findNode(int d) { 
//We will start at the head and then traverse through the entire list to find a Node where data = d 
    Node* start = head; 
    if (start == NULL) { 
     cout<<"No element in the List" <<endl; 
     return NULL; 
    } 
    // If head is the Node we are looking for 
    if (start->data = d) { 
     cout<< "Node found with matching data : " << start << endl; 
     return start; 
    } 
    //While next pointer is not null, traverse to search for a match.s 
    while (start->next != NULL) { 
     start = start->next; 
     if (start->data == d) { 
      cout<< "Node found with matching data : " << start << endl; 
      return start; 
     } 
    } 

    cout << "No node found with matching data = " << d <<endl; 
    return NULL; 
}; 
}; 
+0

、このような問題を解決するための適切なツールは、あなたのデバッガです。スタックオーバーフローを尋ねる前に、コードを一行ずつ進める必要があります。詳しいヘルプは、[小さなプログラムをデバッグする方法(Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)を参照してください。最低限、問題を再現する[最小、完全、および検証可能](http://stackoverflow.com/help/mcve)の例と、その問題を再現するためのデバッガ。 –

+1

この問題を見つける正しいツールは、コンパイラです。 **警告**に注意してください! – Amit

+1

DLLはしばしば** d ** ynamic ** l ** ink ** ** l **ライブラリに関連付けられているので、DLLから名前を変更することをお勧めします。 –

答えて

3
start->data = d 

この行2番目のブロックは>データをスタート - するDを割り当てるのではなく2を比較した場合。

2

これはconstについて学ぶのに適しています。

Node* findNode(int d) { 
//We will start at the head and then traverse through the entire list to find a Node where data = d 
    Node* start = head; 
    if (start == NULL) { 
     cout<<"No element in the List" <<endl; 
     return NULL; 
    } 
    // If head is the Node we are looking for 
    if (start->data = d) { 
     cout<< "Node found with matching data : " << start << endl; 
     return start; 
    } 

この関数は、リストへ書き込みアクセスを持っており、あなたはそれを望んでいません。 if文残念ながら、あなたが最後にこのアクセス権を乱用:

if (start->data = d) { 

このコードはdstart->dataの値を代入し、それに割り当てられた値がnullではなかったかどうかをテストします。

我々は簡単にconstとしてこの機能をマークすることができます

//////////////////////vvvvv///////////////// 
Node* findNode(int d) const { 
//We will start at the head and then traverse through the entire list to find a Node where data = d 
    Node* start = head; 
    if (start == NULL) { 
     cout<<"No element in the List" <<endl; 
     return NULL; 
    } 
    // If head is the Node we are looking for 
    if (start->data = d) { 
     cout<< "Node found with matching data : " << start << endl; 
     return start; 
    } 

、今の場合は、コンパイルエラーが発生します。

Aは、以下のようになりますあなたのコードのバージョンをクリーンアップ:

#include <iostream> 

struct Node { 
    int data_; 
    Node* next_ { nullptr }; 
    Node* prev_ { nullptr }; 

    Node(int data) : data_(data) {} 
}; 


struct DLL { 
    Node* head_ { nullptr }; 
    Node* tail_ { nullptr }; 
    int size_ { 0 }; 

    //Adding a Node to the Doubly LL 
    void addNode(Node* node) { 
     //If LL is empty add the first Node 
     if (tail_ == nullptr) { 
      tail_ = node; 
      head_ = node; 
      node->prev_ = node->next_ = nullptr; 
     } 
     //Else add add node to the tail. Connect n to the tails next and make n the tail 
     else { 
      tail_->next_ = node; 
      node->prev_ = tail_; 
      tail_ = node; 
      node->next_ = nullptr; 
     } 

     size_++; 
    } 

    //Finding a random Node in the linked List 
    //It will return the Node with the FIRST occurrence where data = d 
    Node* findNode(int data) const { 
     //We will start at the head and then traverse through the entire list to find a Node where data = d 
     //While next pointer is not null, traverse to search for a match.s 
     for (Node* start = head_; start != nullptr; start = start->next_) { 
      if (start->data_ == data) { 
       std::cout << "Node found with matching data : " << start << '\n'; 
       return start; 
      } 
     } 

     std::cout << "No node found with matching data = " << data << '\n'; 
     return nullptr; 
    } 
}; 

int main() 
{ 
    DLL dll; 

    Node n1(1), n3(3), n5(5); 
    dll.addNode(&n1); 
    dll.addNode(&n3); 
    dll.addNode(&n5); 

    if (dll.findNode(1) != &n1) 
     std::cerr << "wrong result for findNode(1)\n"; 
    if (dll.findNode(2) != nullptr) 
     std::cerr << "wrong result for findNode(2)\n"; 
    if (dll.findNode(3) != &n3) 
     std::cerr << "wrong result for findNode(3)\n"; 
    if (dll.findNode(4) != nullptr) 
     std::cerr << "wrong result for findNode(4)\n"; 
    if (dll.findNode(5) != &n5) 
     std::cerr << "wrong result for findNode(5)\n"; 
} 

ライブデモ:http://ideone.com/X34EgY