【问题标题】:Error in this linked list implementation此链表实现中的错误
【发布时间】:2017-01-25 04:32:21
【问题描述】:

我目前正在自学编程的数据结构方面,其中包括链表。我编写了一个 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 FOUND\n";
    } else {
    cout << "Search value " << searchValue << " was NOT FOUND\n";
    }
    searchValue = 40;
    nodePtr = ListSearch (searchValue, head );
    if (nodePtr != NULL) {
    cout << "Search value " << searchValue << " was FOUND\n";
    } else {
    cout << "Search value " << searchValue << " was NOT FOUND\n";
    }

    // 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;
}

EDIT 我修复了 ListSearch 功能。它不再发出“.cpp 停止工作”弹出窗口了。但是,输出仍然不正确,并且 searchValue 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 . . .

【问题讨论】:

  • 你能分享你的输出吗?
  • DeleteLastNode 中可疑地没有字符“*L =”。

标签: c++ pointers data-structures linked-list nodes


【解决方案1】:

不幸的是,我没有足够的声誉来发表评论,所以我会发布这个作为答案。我不确定您遇到了什么错误,但代码中有一些错误。搜索功能不检查列表的末尾,即在遍历链表时测试 NULL。它需要这样做,我认为分别进行值比较然后返回正确的节点。

【讨论】:

  • 哦,我明白了,我修复了 ListSearch 功能。但我仍然没有得到正确的输出(我已经编辑了上面的帖子)。
  • 插入第二个节点时,InsertNewLast 中还有另一个错误。代码在 else 之后通过并覆盖列表中的 10,因此它不再存在。此外,搜索功能仍然有点问题,因为第一个 if 现在是多余的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-10
  • 1970-01-01
  • 1970-01-01
  • 2020-01-03
  • 1970-01-01
  • 2020-01-26
相关资源
最近更新 更多