【问题标题】:Swap Adjacent nodes in a Linked List Only by Manipulating pointers仅通过操作指针交换链表中的相邻节点
【发布时间】:2015-07-10 09:40:13
【问题描述】:

我正在尝试交换链表的相邻节点,即

1->2->3->4->5 变成 2->1->4->3->5

我的功能是:

node * swapper(node * &head)
{
    if (head == NULL || head->next == NULL) return head;

    node * t = head;
    head= head->next;
    head->next = t;
    t->next = head->next;

    node *previous = head->next->next, *current = previous->next;

    while (current!=NULL&&previous!=NULL)
    {
        node * t1 = current,*t2=previous;
        current->next = previous;
        previous->next = t1->next;
        previous = t1->next;
        current = previous->next;
    }

    return head;
}

我知道这可以通过交换值来完成,但我必须在常量空间中完成它并且不交换值。

我找不到为什么我的功能不起作用。

【问题讨论】:

  • 这不是MCVE,因此其他人更难找出它不起作用的原因。如果您告诉我们它有什么问题,那将是一个好的开始。是否存在编译/运行时错误?它是否运行但没有产生预期的输出?
  • 我的建议是你先在纸上执行算法。将每个节点画成一个框,将链接(指针)画成带标签的箭头,并尝试先在纸上弄清楚如何做。然后当你有一些你认为可行的东西时,将算法转移到代码中。如果代码没有按预期工作,请在调试器中运行并逐行逐行执行代码,同时跟踪所有指针及其指向的内容(在纸上写下来也有帮助)。
  • @tobi303 好的,我会编辑它。谢谢。

标签: c++ linked-list swap


【解决方案1】:

我注意到的第一件事是你需要交换这两行:

head->next = t;
t->next = head->next;

因为你说的是​​ head->next = t 所以你失去了与链表其余部分的连接。

另外,在循环内部。有几个错误: 1-您在获取上一个之前更改了当前的下一个,这意味着您丢失了链接(如上) 2- 您没有将它们连接到它们之前的节点。

【讨论】:

    【解决方案2】:

    我的五分钱。:)

    这是一个演示程序,展示了如何编写递归函数。

    #include <iostream>
    #include <functional>
    
    struct node
    {
        int data;
        struct node *next;
    } *head;
    
    void push_front( node * &head, int x )
    {
        head = new node { x, head };
    }
    
    void display( node *head )
    {
        for ( node *current = head; current; current = current->next )
        {
            std::cout << current->data << ' ';
        }
        std::cout << std::endl;
    }
    
    node * swapper( node * &head )
    {
        if ( head && head->next )
        {
            node *tmp = std::exchange(head, head->next );
            std::exchange( tmp->next, std::exchange( tmp->next->next, tmp ) );
            swapper( head->next->next );
        }
    
        return head;
    }
    
    int main()
    {
        const int N = 10;
    
        for ( int i = N; i != 0; ) push_front( head, --i );
    
        display( head );
    
        display( swapper( head ) );
    }    
    

    程序输出如下

    0 1 2 3 4 5 6 7 8 9 
    1 0 3 2 5 4 7 6 9 8 
    

    考虑到并非所有编译器都支持函数std::exchange。所以你需要自己写。:)

    如果按以下方式写main

    int main()
    {
        const int N = 10;
    
        for ( int i = N; i != 0; ) push_front( head, --i );
    
        display( head );
    
        for ( node **current = &head; *current && ( *current )->next; current = &( *current )->next )
        {
            swapper( *current ); 
        }
    
        display( head );
    }    
    

    那么可以得到以下有趣的输出。:)

    0 1 2 3 4 5 6 7 8 9 
    1 3 5 7 9 8 6 4 2 0 
    

    【讨论】:

      【解决方案3】:
      node * swapper(node * &head)
      {
          if (head == NULL || head->next == NULL) return head;
      
          node * t = head;
          head= head->next;
      
          t->next = head->next;
          head->next = t;
      
          node *previous = head->next->next, *current,*parent=head->next;
      
          while (previous&&(current = previous->next))
          {
              node * t1 = current,*t2=previous;
              previous->next = t1->next;
              current->next = previous;
              parent->next= current;
      
              previous = t2->next;
              //current = previous->next;
              parent=t2;
          }
      
          return head;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多