简体   繁体   English

交换双链表

[英]Swap Doubly Linked List

Okay. 好的。

So I've been trying to implement a doubly-linked list with a swap function. 因此,我一直在尝试使用交换功能来实现双向链接列表。

I've found many sites online that explain how to do this - and in almost all of them (including a few from Stack Overflow), an argument begins in the comments about whether or not the implementation is correct. 我在网上找到许多网站来解释如何执行此操作-几乎在所有网站(包括Stack Overflow中的一些网站)中,都会在注释中加入一个关于实现是否正确的论点。 I have tried several of them to no avail. 我尝试了其中的几个都无济于事。 In other words, I can't find a single definitive version of this algorithm. 换句话说,我找不到该算法的单个确定版本。

I have tried at least 4-5 different algorithms from those I found online, as well as a few I tried to sort out myself, but I keep getting issues with the elements being sorted incorrectly. 我尝试了至少4-5种与在网上找到的算法不同的算法,以及尝试对自己进行分类的几种算法,但是由于元素排序不正确,我仍然遇到问题。 I am completely at my wits end. 我完全不知所措。

Here is the most recent version I tried, which does not work: 这是我尝试的最新版本,该版本不起作用:

template <typename T>
void PriorityQueue<T>::swap(Node * n1, Node * n2){
    if(n1 == n2)
        return;
    if(n1 == 0 || n2 == 0){
        std::cout << "\nPRIORITYQUEUE ERROR: Trying to swap with non-existent node\n";
        return;
    }

    Node * temp = new Node;

    temp->prev = n1->prev;
    temp->next = n1->next;

    n1->prev = n2->prev;
    n1->next = n2->next;

    n2->prev = temp->prev;
    n2->next = temp->next;

    if(n1->next != 0)
        n1->next->prev = n2;
    if(n1->prev != 0)
        n1->prev->next = n2;
    if(n2->next != 0)
        n2->next->prev = n1;
    if(n2->prev != 0)
        n2->prev->next = n1;

    delete temp;

}

I am beyond frustrated. 我无比沮丧。 Would somebody please, please, please help me out by showing me a complete, working algorithm. 请有人帮我,向我展示完整,有效的算法,以帮助我。 Not linking to a page with a broken algorithm and comments on how to fix it (which I have tried a few times now), but just the algorithm. 不会链接到算法损坏的页面,而不会评论如何修复它(我已经尝试过几次了),只是算法。 It doesn't even need to be code, just an algorithm. 它甚至不需要是代码,只需一个算法即可。 Please. 请。 I am so tired of trying to fix this and so behind schedule on this project and I will be so eternally grateful if someone out there can definitely help me solve this once and for all. 我非常厌倦尝试解决此问题,因此在该项目的进度落后于计划,如果那里有人可以肯定地帮助我彻底解决这一问题,我将永远感激不已。

Your problem in last lines. 最后一行您的问题。 I corrected them: 我更正了它们:

if(n1->next != 0)
    n1->next->prev = n1; // n2;
if(n1->prev != 0)
    n1->prev->next = n1; // n2;
if(n2->next != 0)
    n2->next->prev = n2; // n1;
if(n2->prev != 0)
    n2->prev->next = n2; // n1;

In the beginning of the function you have two nodes n1 and n2 with pointers to other elements n1p , n1n , n2p , n2n . 在功能的开始你有两个节点n1n2与指向其他元素n1pn1nn2pn2n

<-| p |  <-| p |  <-| p |
  |n1p|    | n1|    |n1n|
  | n |->  | n |->  | n |->

<-| p |  <-| p |  <-| p |
  |n2p|    | n2|    |n2n|
  | n |->  | n |->  | n |-> 

In the first part with temp variable you swap pointers of n1 and n2 nodes. 在第一部分中,使用temp变量交换n1n2节点的指针。 So you come to situation like this. 所以你会遇到这样的情况。

<-| p |  <-| p |  <-| p |
  |n1p|    | n2|    |n1n|
  | n |->  | n |->  | n |->

<-| p |  <-| p |  <-| p |
  |n2p|    | n1|    |n2n|
  | n |->  | n |->  | n |->

And you problem was that you incorrectly change pointers for n1p , n1n , n2p and n2n . 你的问题是,你不正确更改指针n1pn1nn2pn2n Take a look in example with sinplified class Node and corrcted swap function. 让我们看一下带有示例化类Node和正确的swap函数的示例

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

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