链表搜索功能不能正常工作
Linked list search function not working properly
我正在使用 C++ 中的方法(例如添加节点、遍历和搜索)创建一个 LinkedList class。在实现搜索功能时,它似乎无法正常工作,因为它在链表中找不到值,而实际上它在链表中。代码如下所示。
#include <iostream>
class Node {
public:
int value;
Node* next;
Node(int value, Node* next) {
this->value = value;
this->next = next;
}
};
class LinkedList {
public:
Node* head;
Node* tail;
LinkedList() {
this->head = nullptr;
this->tail = nullptr;
}
LinkedList(Node* node) {
this->head = node;
this->tail = node;
}
void addNodeFront(Node* node) {
if(head==nullptr && tail==nullptr) {
this->head = node;
this->tail = node;
return;
}
this->tail = this->head;
this->head = node;
node->next = tail;
}
void addNodeBack(Node* node) {
if(head==nullptr && tail==nullptr) {
this->head = node;
this->tail = node;
return;
}
this->tail->next = node;
this->tail = node;
}
void addNodeAfterNode(Node* prevNode, Node* node) {
node->next = prevNode->next;
prevNode->next = node;
}
bool searchVal(int val) {
while(this->head != nullptr) {
if(this->head->value == val) return true;
this->head = this->head->next;
}
return false;
}
void deleteNode(Node* node) {
Node* prevNode = this->head;
while(prevNode->next != node) {
}
}
void traverseLinkedList() {
while(this->head!=nullptr) {
std::cout << this->head->value << "->";
this->head = this->head->next;
}
std::cout << "\n";
}
void sortLinkedList() {
}
};
int main() {
Node node1(2,nullptr);
Node node2(4,nullptr);
Node node3(3,nullptr);
LinkedList ls;
ls.addNodeFront(&node1);
ls.addNodeBack(&node3);
ls.addNodeAfterNode(&node3, &node2);
ls.traverseLinkedList();
if(ls.searchVal(4)) std::cout << "value found\n";
else std::cout << "value not found\n";
}
当我在 main
函数中调用 searchVal()
函数时,它输出 value not found
而值 4 在链表中。我的代码有什么问题?
When I call the searchVal() function inside the main function it
outputs value not found while value 4 is inside the linked list. What
is wrong with my code?
就在调用 searchVal(4)
之前调用 traverseLinkedList()
,traverseLinkedList()
的实现方式是 returns,this->head
NULL
,这意味着此时你的链表是空的(并且你已经泄漏了内存)。您需要修改 traverseLinkedList()
和 searchVal()
以不更改 this->head
(或 LinkedList
对象的任何其他 member-variables)的值,以便它们不' 修改列表的状态作为副作用。
我正在使用 C++ 中的方法(例如添加节点、遍历和搜索)创建一个 LinkedList class。在实现搜索功能时,它似乎无法正常工作,因为它在链表中找不到值,而实际上它在链表中。代码如下所示。
#include <iostream>
class Node {
public:
int value;
Node* next;
Node(int value, Node* next) {
this->value = value;
this->next = next;
}
};
class LinkedList {
public:
Node* head;
Node* tail;
LinkedList() {
this->head = nullptr;
this->tail = nullptr;
}
LinkedList(Node* node) {
this->head = node;
this->tail = node;
}
void addNodeFront(Node* node) {
if(head==nullptr && tail==nullptr) {
this->head = node;
this->tail = node;
return;
}
this->tail = this->head;
this->head = node;
node->next = tail;
}
void addNodeBack(Node* node) {
if(head==nullptr && tail==nullptr) {
this->head = node;
this->tail = node;
return;
}
this->tail->next = node;
this->tail = node;
}
void addNodeAfterNode(Node* prevNode, Node* node) {
node->next = prevNode->next;
prevNode->next = node;
}
bool searchVal(int val) {
while(this->head != nullptr) {
if(this->head->value == val) return true;
this->head = this->head->next;
}
return false;
}
void deleteNode(Node* node) {
Node* prevNode = this->head;
while(prevNode->next != node) {
}
}
void traverseLinkedList() {
while(this->head!=nullptr) {
std::cout << this->head->value << "->";
this->head = this->head->next;
}
std::cout << "\n";
}
void sortLinkedList() {
}
};
int main() {
Node node1(2,nullptr);
Node node2(4,nullptr);
Node node3(3,nullptr);
LinkedList ls;
ls.addNodeFront(&node1);
ls.addNodeBack(&node3);
ls.addNodeAfterNode(&node3, &node2);
ls.traverseLinkedList();
if(ls.searchVal(4)) std::cout << "value found\n";
else std::cout << "value not found\n";
}
当我在 main
函数中调用 searchVal()
函数时,它输出 value not found
而值 4 在链表中。我的代码有什么问题?
When I call the searchVal() function inside the main function it outputs value not found while value 4 is inside the linked list. What is wrong with my code?
就在调用 searchVal(4)
之前调用 traverseLinkedList()
,traverseLinkedList()
的实现方式是 returns,this->head
NULL
,这意味着此时你的链表是空的(并且你已经泄漏了内存)。您需要修改 traverseLinkedList()
和 searchVal()
以不更改 this->head
(或 LinkedList
对象的任何其他 member-variables)的值,以便它们不' 修改列表的状态作为副作用。