交换链表的相邻元素
Swapping adjacent elements of linked list
下面是我递归交换链表中相邻元素的代码。交换后,我会丢失指向每秒钟一个元素的指针。输入是1->2->3->4->5->6->7,我预期输出是2->1->4->3->6->5->7,但我的输出是1->3->5->7。
void nodelist::swap(node* head)
{
node* temp = head->next;
if (head->next!= nullptr)
{
node* temp2 = temp->next;
temp->next = head;
head->next = temp2;
head = head->next;
temp = nullptr;
temp2 = nullptr;
swap(head);
}
}
如有任何帮助,我们将不胜感激,提前表示感谢。
事实上,只交换节点的数据成员就足够了。没有必要交换指针本身。
然而,如果使用您的方法,那么该功能可能看起来像
void SwapList( node *head )
{
if ( head != nullptr && head->next != nullptr )
{
node *next = head->next;
std::swap( *head, *next );
std::swap( head->next, next->next );
SwapList( head->next->next );
}
}
这是一个演示程序
#include <iostream>
#include <utility>
struct node
{
int value;
node *next;
};
node * AddNode( node *head, int value )
{
head = new node { value, head };
return head;
}
void PrintList( node *head )
{
for ( ; head != nullptr; head = head->next )
{
std::cout << head->value << ' ';
}
}
void SwapList( node *head )
{
if ( head != nullptr && head->next != nullptr )
{
node *next = head->next;
std::swap( *head, *next );
std::swap( head->next, next->next );
SwapList( head->next->next );
}
}
int main()
{
node *head = nullptr;
for ( int i = 10; i != 0; )
{
head = AddNode( head, --i );
}
PrintList( head );
std::cout << std::endl;
SwapList( head );
PrintList( head );
std::cout << std::endl;
return 0;
}
输出为
0 1 2 3 4 5 6 7 8 9
1 0 3 2 5 4 7 6 9 8
您可以将显示的函数用作函数的模板(或基础)。
无递归:
void swap(node **head)
{
while (*head && (*head)->next)
{
node* tmp = *head;
*head = tmp->next;
tmp->next = (*head)->next;
(*head)->next = tmp;
head = &tmp->next;
}
}
调用swap( & list_head_ptr)
。
或者,您可以通过引用指针来传递头指针,并使用指向指针成员的本地指针:
void swap(node*& head)
{
node **pp = &head;
while (*pp && (*pp)->next)
{
node* tmp = *pp;
*pp = tmp->next;
tmp->next = (*pp)->next;
(*pp)->next = tmp;
pp = &tmp->next;
}
}
并作为CCD_ 2调用。任何一种方法都有效。
使用递归:
void nodelist::swap(node** head) {
if (!*head || !(*head)->next) return;
node* const sw = (*head)->next;
(*head)->next = sw->next;
sw->next = *head;
*head = sw;
swap(&(sw->next->next));
}
如果head
是存储firstNode (value=1)
地址的指针,则尝试以下函数:
void nodelist::swap(node* head){
node* temp = head->next; //head->next is first-node which needs to switch with it's next node
if (temp!= nullptr && temp->next!=nullptr){
head->next=temp->next; //move second node to first
temp->next = head->next->next; //put second's next in first's
head->next->next = temp; //and first will be second's next
temp = nullptr; // swaping done
swap(head->next->next); //do it for next couple
}
}
http://coliru.stacked-crooked.com/a/e1cc0d02b5599da4
或
http://coliru.stacked-crooked.com/a/a1e200b687825d80
如果head
本身就是firstNode (value=1)
,那么通过值传递head
将不起作用,您需要通过地址/引用传递它或者按照以下链接中的方式进行传递:
http://coliru.stacked-crooked.com/a/a1e200b687825d80
相关文章:
- 打印所有链表的元素 (C++)
- 如何实现 Front() 方法以返回模板化双向链表C++的第一个元素?
- 在链链表中手动插入第五个元素
- 将数组的元素添加到链表中
- 为什么我在尝试在单向链表中打印元素时会出现这个永无止境的循环
- 如何将元素附加到链表?
- 仅显示链表的最后一个元素
- 从 c++ 中的链表中获取元素
- C++删除链表中的元素
- 我正在尝试计算链表中当前元素的数量
- 为什么将函数的返回类型从结构节点*更改为void后,链表的元素没有显示create_ll和显示?
- 如何从几乎排序的链表中分离放错位置的元素?
- 如何比较链表的元素?
- 链表 - 元素在删除后出现
- C++链表最后一个元素的迭代器?
- 如何找到双向链表的最大元素?
- 使用带有结构结构作为 C++; 元素的单向链表
- 如何检查单向链表的每个 3 个元素,然后删除其中一些元素?
- 将元素添加到 Head 的双链表
- 算法-统一随机元素链表