C++에서 이중 연결 목록을 구현하려고하는데 추가 함수가 제대로 작동하지만 노드 찾기 함수가 목록을 수정하고 있습니다. insertAfter, delete와 같은 다른 모든 함수는이 찾기 함수에 의존하므로 예상대로 작동하지 않습니다.링크 목록 검색 함수 수정 목록
저는 C++을 처음 사용하기 때문에 포인터를 완전히 이해하지 못합니다. C++로 Java 프로그램을 복제하려했습니다. 나는 find 함수에서 헤드 노드에 대한 포인터가 문제를 일으키는 지 확실히 알고 있지만 어떻게 완전히 이해하지는 못합니다. 블록이 두 가지를 비교보다는> 데이터 시작 - D를 할당하는 경우 두 번째에
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 ** ** ** ** ** ** ** ** ** 라이브러리와 연결되기 때문에 DLL에서 이름을 변경하는 것이 좋습니다. –