在双向链表中添加函数导致无限循环

时间:2016-10-07 16:54:26

标签: c++ linked-list infinite-loop traversal nullptr

我在编写双向链表时遇到了麻烦。问题是当我检查链接是否为nullptr时,我的Add函数会导致无限循环。当我不在时,它给了我一个错误。我一直试图解决这个问题,因为我的生活无法解决这个问题。以下是添加方法:

    void Add(string n, int w) //Method to add a node to the Linked List and maintain the order.
{

    node * nd = new node(n, w, nullptr, nullptr);

    if (nHead == nullptr && wHead == nullptr) //If there is nothing in the Linked List
    {
        nHead = nd; //Add a node
        wHead = nd;
    }

    else //If there is something in the linked List
    {
        node * ntraverse = nHead; //variable to traverse down the name links

        while (nd->name > ntraverse->name && ntraverse->wLink != nullptr)
        {
            ntraverse = ntraverse->nLink; // Traverses down the name links until nd's name is smaller than a links
        }

        nd->nLink = ntraverse; // Here, the namelink for nd is set to ntraverse, since ntraverse is less than or equal to nlink
        ntraverse->nLink = nd; // So here, since nd is the new value appended to the rest of the list, we set ntraverse = nlink.

                               // note at this point, we have not handled weight

        node * wtraverse = wHead; //variable to traverse down the weight links

        while (nd->weight > wtraverse->weight && wtraverse->wLink != nullptr)
        {
            wtraverse = wtraverse->wLink; // Traverses down the weight links until nd's weight is smaller than a links
        }

        nd->wLink = wtraverse; // Here, the namelink for nd is set to ntraverse, since ntraverse is less than or equal to nlink
        wtraverse->wLink = nd; // So here, since nd is the new value appended to the rest of the list, we set ntraverse = nlink.

        //at this point, nd holds both the correct nlink and wlink

    }

    cout << "Added: " << nd->name << " " << nd->weight << "\n";
    cout << "Current nlist:\n";
    nPrint();
    cout << "Current wlist:\n";
    wPrint();

    size++; //increment size

}

非常感谢任何帮助。如果您需要我回答任何问题,请告诉我。节点工作正常,它存储4个值:name,weight,nLink和wLink,其中nLink保持列表按名称排序,wLink保持列表按重量排序。对于LinkedList,nHead是名称head,wHead是权重头。

再次感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

您正在将wLink和nLink混合在一起

node * ntraverse = nHead; //variable to traverse down the name links

while (nd->name > ntraverse->name && ntraverse->wLink != nullptr)

在上文中,您将遍历名称链接并进行测试以查看您是否位于权重列表的末尾。

这不是一个双向链表,它是两个单链表,你应该单独处理每个列表。

换句话说,您的代码应如下所示:

    void Add(string n, int w) //Method to add a node to the Linked List and maintain the order.
{

    node * nd = new node(n, w, nullptr, nullptr);

    if (nHead == nullptr ) //If there is nothing in the Linked List
    {
        nHead = nd; //Add a node
    }


    else //If there is something in the linked List
    {
       /* Code to handle nLink with no mention of wLink */
    }

    if (wHead == nullptr ) //If there is nothing in the Linked List
    {
        wHead = nd; //Add a node
    }


    else //If there is something in the linked List
    {
       /* Code to handle wLink with no mention of nLink */
    }

}

当然,理想的解决方案是编写链表类....

答案 1 :(得分:0)

所以我弄清楚问题是什么。在每个循环结束时,我有

    nd->wLink = wtraverse; // Here, the namelink for nd is set to ntraverse, since ntraverse is less than or equal to nlink
    wtraverse->wLink = nd; // So here, since nd is the new value appended to the rest of the list, we set ntraverse = nlink.

这创造了一个循环循环。 nd的链接存储了wtraverse,而wtraverse的链接存储了nd,这意味着其中一个链接将指向列表的另一部分。

关于双重链接列表术语的语义参数,我的数据结构教授指的是一个数据结构,其中节点有两个链接作为双向链接列表。无论它是否正确,争论语义都没有什么帮助,也没有做任何事情来解决这个问题。