简体   繁体   English

C ++交换链表中的相邻节点

[英]C++ Swapping adjacent nodes in a linked list

I'm having some trouble swapping adjacent nodes in a singly linked list. 我在交换单链列表中的相邻节点时遇到一些麻烦。 Here's my swap function: 这是我的交换函数:

void swap(std::shared_ptr<ListItem> root, int indexA, int indexB)
{
    if (indexA == 0)
    {
        std::shared_ptr<ListItem> A = root;
        std::shared_ptr<ListItem> B = A->next;

        A->next = B->next;
        B->next = A;
        root = B;
    }
    else if (indexB == 0)
    {
        std::shared_ptr<ListItem> B = root;
        std::shared_ptr<ListItem> A = B->next;

        B->next = A->next;
        A->next = B;
        root = A;
    }
    else
    {
        std::shared_ptr<ListItem> preA = GetNode(root, indexA - 1);
        std::shared_ptr<ListItem> preB = GetNode(root, indexB - 1);
        std::shared_ptr<ListItem> A = preA->next;
        std::shared_ptr<ListItem> B = preB->next;
        std::shared_ptr<ListItem> temp = B->next;

        preA->next = B;
        A->next = temp;
        B->next = A;
    }
}

Now as you can see this code only handles adjacent nodes. 现在,您可以看到该代码仅处理相邻节点。 That is because I'm only using it in my sort function: 那是因为我只在排序函数中使用它:

void LinkedList::sort() {
    for (int i = 0; i <= this->getSize(); i++)
    {
        int j = i;
        while (j > 0 && getItem(j) < getItem(j - 1))
        {
            swap(root, (j - 1), j);
            j = j - 1;
        }
    }
}

So every time the swap function is ran, the nodes sent in will be adjacent. 因此,每次运行交换功能时,发送的节点将相邻。 My problem is that the way I'm swapping right now I lose nodes along the way, the link between them is broken somewhere but I really don't understand why or where. 我的问题是,我现在交换的方式一路上会丢失节点,它们之间的链接在某处断开,但我真的不知道为什么或在哪里。 My guess is that I need to use a temporary node but since, again, I don't understand why or where the link is being broken I also don't know where I would need to use the temporary node. 我的猜测是我需要使用一个临时节点,但是由于又一次,我不知道链接断开的原因或位置,所以我也不知道该在哪里使用临时节点。

Also the preB node is never used at the moment, that's a remnant from quite a few failed attempts at a fix. 同样,preB节点目前从未使用过,这是很多次尝试修复失败的结果。 Any tips would be greatly appreciated! 任何提示将非常感谢!

So it turns out my problem was something I had never even considered before. 事实证明,我的问题是我以前从未考虑过的问题。 It's this line in the sort function: 这是sort函数中的这一行:

for (int i = 0; i <= this->getSize(); i++)

That will always make it go one step too far, and I had added a sort of failsafe to getItem() that made it hard to pin down. 这总是会使它走得太远了,我在getItem()中添加了一种故障保护功能,很难固定下来。 Basically in getItem() I said that if the node I'm looking for is a nullptr, return 0. Which is really dumb since it makes it seem as if the problem is in swap() rather than sort(). 基本上在getItem()中,我说过,如果我要查找的节点是nullptr,则返回0。这确实是愚蠢的,因为它使问题似乎出在swap()而不是sort()中。

So I changed that one line to: 所以我将那一行更改为:

for (int i = 0; i < this->getSize(); i++)

And it now works. 现在就可以了。 Really dumb mistake and I apologize for wasting people's time. 真是愚蠢的错误,对于浪费人们的时间我深表歉意。 Also thank you very much @manni66 for helping me figure this out! 也非常感谢@ manni66帮助我解决了这个问题!

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

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