【问题标题】:(C++) Getting inconsistent results while using Nodes/Linked Lists.(C++) 使用节点/链表时得到不一致的结果。
【发布时间】:2018-10-27 04:21:27
【问题描述】:
#include <iostream>
#include <memory>
using namespace std;

struct Node
{
    int data;
    Node* next;
};

void append(Node*&, int);
void printList(Node*);
void insertNode(Node*&, int, int);
void searchList(Node*, int, int);
int main()
{
    Node* head = nullptr;

    int initialCount = -1, userInput, newNodeLoc = -1, newNodeVal, searchVal;

                        /// INITIALIZE LIST
    while(initialCount <= 0)
    {
        cout<<"Enter the number of initial nodes (must be at least 1): ";
        cin>>initialCount;
    }
    cout<<endl;

    for(int i = 0;i<initialCount;i++)
    {
        cout<<"Enter a number: ";
        cin>>userInput;
        append(head,userInput);
    }
    cout<<endl;

    cout<<"Here are the initial values in the linked list: "<<endl;
    printList(head);

    cout<<"\nEnter a number for a new node to insert to the linked list: ";
    cin>>newNodeVal;
    cout<<endl;
    cout<<"Enter which position you want to insert it (ex. "<<head->data<<" is at pos 1): ";
    cin>>newNodeLoc;
    while((newNodeLoc<=0 || newNodeLoc>initialCount))
    {
        cout<<"New position must be greater than 1 and less than " << initialCount+1 <<": ";
        cin>>newNodeLoc;
    }
    newNodeLoc--;
    insertNode(head, newNodeVal, newNodeLoc);

    cout<<"\nHere is the updated linked list: "<<endl;
    printList(head);

                        /// SEARCH
    cout<<"\nEnter the number that you want to search for in the list: ";
    cin>>searchVal;
    cout<<endl;

    initialCount++;
    cout<<initialCount;
    searchList(head,searchVal,initialCount);

    return 0;
}
void printList(Node* head)
{
    Node *n = head;
    cout<<n->data<<endl;
    while(n->next != nullptr)   // print out all nodes values'
    {
        cout << n->next->data<<endl;
        n = n->next;
    }
}
void append(Node*& head, int val)
{
    Node* temp = new Node;
    temp->data = val;
    temp->next = nullptr;

    Node* ptr = head;

    if(head == nullptr)                 // check if list is empty
    {
        head = temp;
    }
    else
    {
        while(ptr->next != nullptr)     // if list isn't empty, get to last element set it equal to temp
        {
            ptr = ptr->next;
        }
        if(ptr->next == nullptr)
        {
            ptr->next = temp;
        }
    }

    delete temp;
    temp = nullptr;

}
void insertNode(Node*& head, int val, int loc)
{
    Node* temp = new Node;
    Node* prevLoc = new Node;
    Node* curr = head;


    temp->data = val;

    int tempPos = 0;

    while(curr->next != nullptr && tempPos != loc)
    {
        prevLoc = curr;
        curr = curr->next;
        tempPos++;
    }

    prevLoc->next = temp;
    temp->next = curr;

    delete temp;
    delete prevLoc;
    curr = nullptr;
    prevLoc = nullptr;
}
void searchList(Node* head, int sVal, int iCount)
{
    Node* curr = head;
    int index=0;

    while(curr->next != nullptr && curr->next->data != sVal)
    {
        curr = curr->next;
        index++;
    }

    cout<<index;
    cout<<iCount;
    if(index != iCount)
    {
        cout<<"Number found at index "<<index<<" in the linked list!";
    }
    if(index-1 == iCount)
        cout<<"Number could not be found in this linked list.";

    delete curr;
    curr = nullptr;

}

你好!我正在尝试实现 append/prntlist/insertnode/search 函数,但编译结果非常不一致。有时代码会运行良好。有时代码会随机中断。其他时候,它会在无限循环中打印出数字。我认为这是某处的内存泄漏(在附加/打印函数中),但我不是超级自信。它也可能是一个破坏代码的循环。任何和所有的帮助表示赞赏! 我了解搜索功能不起作用,您可以忽略它。谢谢!

【问题讨论】:

  • 你试过在调试器中运行它吗?你从中学到了什么?

标签: c++ list hyperlink append head


【解决方案1】:

在你的append():

delete temp;

temp 是您的新元素,您刚刚将append() 添加到列表中。现在它是列表的一部分,它会立即获得deleted。再见!

链表最后一个元素的旁边现在指向已删除的内存。随后尝试遍历列表会导致未定义的行为。随后尝试将更多元素附加到列表中只会让事情变得更糟,在deleteed 内存上乱涂乱画(如果无论如何都没有乱涂,到下一个new)并且通常会使一切变得一团糟。

insert() 中的内存分配逻辑同样存在缺陷。情况更糟。两个news 和两个deletes。此外,整体逻辑也是错误的。其表面上的目的是在列表中再插入一个元素,因此它不应该分配两个新节点,只有一个会这样做。 append() 和 insert() 都向列表中添加了一个节点;所以在这两种情况下只需要newed 一个节点。

但总体问题是错误删除了newed 元素,而它们不应该是newed,并且它们继续被使用。获得deleted 后,您将无法使用任何东西。它消失了。它不复存在了。它加入了无形的合唱团。这是一个前对象。但是显示的代码在添加到链接列表后错误地deletes 一个元素,并且从表面上看,它在逻辑上仍然是链接列表的一部分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-19
    • 2018-11-04
    • 1970-01-01
    • 2018-10-12
    • 2013-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多