此链表实现中的错误
Error in this linked list implementation
我目前正在自学编程的数据结构方面,其中包括链表。我编写了一个C++程序,该程序涉及创建列表、插入节点、在列表中搜索值、输出列表和删除它们。由于某种原因,我得到了错误的输出。非常感谢任何形式的帮助或建议。
#include <iostream>
using namespace std;
typedef int DataItem;
struct Node {
DataItem data;
Node *next;
};
Node* ListSearch(DataItem value, Node *head) {
if(head == NULL)
return NULL;
Node *nodePtr = head;
while(nodePtr!= NULL){
if (nodePtr->data == value)
return nodePtr;
nodePtr = nodePtr->next;
}
return NULL;
}
void InsertNewLast(DataItem value, Node **L) {
Node *nodePtr = *L;
if(nodePtr == NULL){
nodePtr = new Node();
nodePtr->data = value;
nodePtr->next = NULL;
*L = nodePtr;
}
else{
while(nodePtr->next!= NULL){ //go through the list
nodePtr = nodePtr->next;
}
nodePtr->next = new Node();
nodePtr->data = value;
}
return;
}
void DeleteLastNode(Node **L) {
Node* nodePtr = *L;
if(nodePtr == NULL)
return;
if(nodePtr != NULL && nodePtr->next != NULL){
Node *newLast = nodePtr;
while(newLast->next->next != NULL){
newLast = newLast->next;
}
delete newLast->next;
newLast->next=NULL;
}
else{
delete nodePtr;
nodePtr = NULL;
}
*L = nodePtr;
}
void PrintList(Node *head) {
Node* nodePtr = head;
if(nodePtr== NULL)
return;
else{
while(nodePtr!=NULL){
cout << "[" << nodePtr->data << "]";
nodePtr = nodePtr->next;
if (nodePtr != NULL)
cout << "->";
}
cout << endl;
return;
}
}
int main () {
Node *head;
Node *nodePtr;
DataItem searchValue;
head = NULL;
// Printing and Inserting...
PrintList(head);
InsertNewLast(10, &head);
PrintList(head);
InsertNewLast(20, &head);
PrintList(head);
InsertNewLast(30, &head);
PrintList(head);
InsertNewLast(40, &head);
PrintList(head);
InsertNewLast(50, &head);
PrintList(head);
// Searching...
searchValue = 10;
nodePtr = ListSearch(searchValue, head);
if (nodePtr != NULL) {
cout << "Search value " << searchValue << " was FOUND" << endl;
} else {
cout << "Search value " << searchValue << " was NOT FOUND" << endl;
}
searchValue = 5;
nodePtr = ListSearch(searchValue, head);
if (nodePtr != NULL) {
cout << "Search value " << searchValue << " was FOUNDn";
} else {
cout << "Search value " << searchValue << " was NOT FOUNDn";
}
searchValue = 40;
nodePtr = ListSearch (searchValue, head );
if (nodePtr != NULL) {
cout << "Search value " << searchValue << " was FOUNDn";
} else {
cout << "Search value " << searchValue << " was NOT FOUNDn";
}
// Deleting and Printing...
DeleteLastNode(&head);
PrintList(head);
DeleteLastNode(&head);
PrintList(head);
DeleteLastNode(&head);
PrintList(head);
DeleteLastNode(&head);
PrintList(head);
DeleteLastNode(&head);
PrintList(head);
return 0;
}
编辑 我修复了列表搜索功能。它不再发出".cpp停止工作"弹出窗口。但是,输出仍然不正确,搜索值 10 显示为未找到。
输出:
[10]
[20]->[0]
[20]->[30]->[0]
[20]->[30]->[40]->[0]
[20]->[30]->[40]->[50]->[0]
Search value 10 was NOT FOUND
Search value 5 was NOT FOUND
Search value 40 was FOUND
[20]->[30]->[40]->[50]
[20]->[30]->[40]
[20]->[30]
[20]
--------------------------------
Process exited after 0.02802 seconds with return value 0
Press any key to continue . . .
不幸的是,
我没有足够的声誉来发表评论,所以我会将其作为答案发布。我不确定您遇到了什么错误,但代码中存在一些错误。搜索函数不检查列表的末尾,即在浏览链表时测试 NULL。它需要这样做,我认为单独进行值比较,然后返回正确的节点。
相关文章:
- 在c++中实现LinkedList时,应出现未处理的错误
- 尝试使用继承和模板实现CRTP.Visual Studio正在生成编译器错误
- 在 c++ 中实现 Trie 时出现分段错误
- C++ 实现模板单例类时出现链接错误
- 内存错误低于在C++年实现埃拉托色尼筛分时的预期
- C++数组队列实现方法错误
- 实现 DFS 在较短的输入下工作正常,但在较大的输入下会抛出分段错误
- 分段 排序函数实现中的错误
- 尝试从头开始实现Leetcode的FizzBuzz多线程问题。收到"libc++abi.dylib: terminating"错误
- 使用 std::forward_list 返回错误的队列实现
- 在我的trie实现中出现分段错误
- 在 c++ 中实现链表时出现分段错误
- 比较迭代器会使程序崩溃,而不会在自定义气泡排序实现中出现错误
- 如何知道C2259 VS 2017错误未实现哪种方法?
- 筛子的埃拉托色尼错误实现
- BAD_ALLOC错误实现向量调整大小函数时
- 头文件中包含.cpp重新定义错误-实现通用堆栈
- 程序没有给出期望的输出.FIFO的错误实现
- 这是卡达内算法的错误实现吗?
- 双链表错误实现Deque