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;
};
};
、このような問題を解決するための適切なツールは、あなたのデバッガです。スタックオーバーフローを尋ねる前に、コードを一行ずつ進める必要があります。詳しいヘルプは、[小さなプログラムをデバッグする方法(Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)を参照してください。最低限、問題を再現する[最小、完全、および検証可能](http://stackoverflow.com/help/mcve)の例と、その問題を再現するためのデバッガ。 –
この問題を見つける正しいツールは、コンパイラです。 **警告**に注意してください! – Amit
DLLはしばしば** d ** ynamic ** l ** ink ** ** l **ライブラリに関連付けられているので、DLLから名前を変更することをお勧めします。 –