【问题标题】:How to swap nodes between two Doubly Linked List completely如何在两个双向链表之间完全交换节点
【发布时间】:2021-07-30 18:50:24
【问题描述】:

大家好,我正在尝试在两个双向链表之间完全交换两个节点(值和地址也是)。只有位于相同位置的节点可以相互交换,即位置 2 的节点只能由另一个 LinkedList 中位置 2 的节点交换。考虑以下 2 个 LinkedList 的示例:

815 102 162 524 
622 101 830 754

假设我们要交换第三个元素,即 162 和 830。交换 LinkedLists 后变为:

815 102 830 524 
622 101 162 754

我已经尝试了以下代码,但它不能替换之前的元素。

void swapNodes(Node* firstListNode, Node* secondListNode)
{
    Node* FirstNodeNext = firstListNode->next;
    Node* FirstNodePrev = firstListNode->previous;
    Node* SecondNodeNext = secondListNode->next;
    Node* SecondNodePrev = secondListNode->previous;
    
    //if the nodes are heads
    if (firstListNode->previous == NULL && secondListNode->previous == NULL)
    {   
        firstListNode->next = SecondNodeNext;
        secondListNode->next = FirstNodeNext;
    }
    // if the nodes are tails
    else if(firstListNode->next == NULL && secondListNode->next == NULL)
    {
        firstListNode->previous = SecondNodePrev;
        secondListNode->previous = FirstNodePrev;
    }
    else
    {
        firstListNode->next = SecondNodeNext;
        firstListNode->previous = SecondNodePrev;

        secondListNode->next = FirstNodeNext;
        secondListNode->previous = FirstNodePrev;
     }
}

我怎样才能完成这项任务?

else if 不会交换前面的元素,例如,如果我们将值 524 和 754 传递给函数,它应该是尾部并执行 else if 语句,因为它没有下一个节点。

交换后应该是:

815 102 162 754
622 101 830 524

代码不会交换之前的节点。

【问题讨论】:

  • 您正在交换包含在firstListNodesecondListNode 中的指针,但您没有更正指向它们的节点。
  • 解决大多数链表问题的最简单方法是通过画图将问题可视化。如果你一步一步地画出这个过程,你会看到你是否删除了以后需要的链接,或者忘记了创建链接。将代码建立在图纸上,在调试时完全按照代码来尝试绘制相同的图片。如果你不能,你通常会确切地知道你哪里出错了。
  • 您不必交换整个列表,您只需重定向节点指向的位置。
  • 为什么要交换这两个列表?我以为你只想交换两个节点。
  • 请提供用于创建两个示例列表并调用此函数的代码。我确实想知道为什么使用 2 个节点指针而不是 indexlists 对象调用此函数。

标签: c++ data-structures linked-list doubly-linked-list


【解决方案1】:

我认为您正在丢失在其他链表中交换的引用,下面是 java 中相同的工作代码 sn-p。请注意,我在这里假设两个链表的长度相同。

public class Application {

    static class Node{
        private int value;
        private Node prev;
        private Node next;

        public Node(int value){
            this.value = value;
        }

        public void print(){
            for(Node node=this; node!=null; node = node.next){
                System.out.print(node.value + "->");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        Node head1 = new Node(815);
        addNodeAtLast(head1, 102);
        addNodeAtLast(head1, 162);
        addNodeAtLast(head1, 524);
        head1.print();

        Node head2 = new Node(622);
        addNodeAtLast(head2, 101);
        addNodeAtLast(head2, 830);
        addNodeAtLast(head2, 754);
        head2.print();

        swapAtIndex(head1, head2, 3);
        head1.print();
        head2.print();
    }

    private static void swapAtIndex(Node head1, Node head2, int index){
        System.out.println("Swaping node at index : "+index);
        if(index == 0){
            Node tmp = head1.next;
            head1.next= head2.next;
            head2.next = tmp;
            return;
        }
        Node linkedList1Ref = head1,  linkedList2Ref = head2;
        for(int i=0; i!=index; ++i, linkedList1Ref = linkedList1Ref.next, linkedList2Ref=linkedList2Ref.next);

        Node temp2Prev = linkedList2Ref.prev;
        Node temp2Nxt = linkedList2Ref.next;

        linkedList1Ref.prev.next = linkedList2Ref; // LinkedList 1 swap
        linkedList2Ref.prev = linkedList1Ref.prev; // LinkedList 1 swap
        if(linkedList2Ref.next != null && linkedList1Ref.next != null) {
            linkedList2Ref.next = linkedList1Ref.next; // LinkedList 1 swap
            linkedList1Ref.next.prev = linkedList2Ref; // LinkedList 1 swap
        }

        temp2Prev.next = linkedList1Ref;
        linkedList1Ref.prev = temp2Prev;
        if(linkedList1Ref.next != null && temp2Nxt != null) {
            linkedList1Ref.next = temp2Nxt;
            temp2Nxt.prev = linkedList1Ref;
        }
    }

    private static void addNodeAtLast(Node head, int value){
        Node temp = new Node(value);
        Node prev = null;
        for(prev=head; prev.next != null; prev=prev.next);
        temp.prev = prev;
        prev.next = temp;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多