(C ++)使用节点/链接列表时获得不一致的结果。

时间:2018-05-17 01:59:22

标签: c++ list hyperlink append head

#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函数,并且我的编译结果非常不一致。有时代码运行正常。有时代码会随机破坏。其他时候它会在无限循环上打印出数字。我认为这是某个地方的内存泄漏(附加/打印功能),但我不是很自信。它也可能是一个破坏代码的循环。任何和所有的帮助表示赞赏! 我知道搜索功能不起作用,所以你可以忽略它。谢谢!

1 个答案:

答案 0 :(得分:1)

append()

delete temp;

temp是您的新元素,您只需append()编辑该列表。现在它已成为列表的一部分,它立即得到delete d。再见!

链接列表的最后一个元素旁边现在指向已删除的内存。随后尝试遍历列表会导致未定义的行为。随后尝试将更多元素添加到列表中会使事情变得更糟,在delete内存上涂鸦(如果它没有被潦草地写下来,那么下一个new)并且通常会一切都很糟糕。

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

但总体问题是new ed元素的错误删除,当它们不应该被new编辑时,它们会继续被使用。获得delete d后,您无法使用任何内容。它已经消失了。它不复存在。它加入了唱诗班 - 隐形。它是一个前对象。但是显示的代码错误地delete是一个元素,它被添加到链接列表后,表面上,它仍然是逻辑上链接列表的一部分。