简体   繁体   English

此链表实现中的错误

[英]Error in this linked list implementation

I'm currently self-learning the Data Structures side of programming, which includes linked lists. 我目前正在自学编程的“数据结构”端,其中包括链表。 I coded a C++ program that involves creating a list, inserting nodes, searching through the list for a value, outputting the list, and deleting them. 我编写了一个C ++程序,其中涉及创建列表,插入节点,在列表中搜索值,输出列表并删除它们。 For some reason, I'm getting the wrong output. 由于某种原因,我得到了错误的输出。 Any kind of help or suggestions are greatly appreciated. 任何帮助或建议都将不胜感激。

#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 I fixed up the ListSearch function. 编辑我修复了ListSearch功能。 It didn't give out the '.cpp stopped working" pop-up anymore. But, the output still isn't right and searchValue 10 comes out as not found. 它不再发出“ .cpp停止工作”的弹出窗口。但是,输出仍然不正确,searchValue 10找不到。

Output: 输出:

[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 . . .

Unfortunately I don't have enough reputation to post a comment so I will post this as an answer. 不幸的是,我没有足够的声誉来发表评论,所以我将其作为答复。 I am not sure what error you are getting but there are some bugs in the code. 我不确定您遇到什么错误,但是代码中存在一些错误。 The search function doesn't check for the end of the list ie test for NULL when walking the linked list. 搜索功能不检查列表的末尾,即在链表走动时测试NULL。 It needs to do this and I think separately do the value comparison then return the correct node. 它需要这样做,我想分别进行值比较,然后返回正确的节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM