简体   繁体   English

在C中的双链表中交换节点

[英]Swapping nodes in double linked list in C

I am trying to swap nodes in a double linked list in C. 我正在尝试在C中的双链表中交换节点。

My list if as it follows: M4,M3,M2,M1. 我的列表如下:M4,M3,M2,M1。 I have a function that computes the dimension of the list and it reads it correctly as 4. 我有一个计算列表尺寸的函数,它可以正确读取为4。

It all breaks down when I try to swap M3 with M2.(hoping to get after traversing it this: M4,M2,M3,M1.) 当我尝试用M2交换M3时,一切都崩溃了(希望遍历它后得到:M4,M2,M3,M1。)

I use the following line in main: (prim is the the first node, M4) 我在main中使用以下行:(primary是第一个节点,M4)

swap_nodes1(prim->pNext, prim->pNext->pNext);

And the following code serves as my swapping function. 以下代码用作我的交换功能。 (Note:i still need to cover the cases when switching the head with another node, or the tail with another node, yet I wanted first to swap some random middle positioned nodes.) (注意:当切换头节点到另一个节点或尾节点到另一个节点时,我仍然需要解决这些情况,但我想先交换一些随机的中间位置的节点。)

void swap_nodes1(pNODE object1, pNODE object2)
{
pNODE temp1,temp2;
temp1->pPrec=object1->pPrec;
temp1->pNext=object1->pNext;

object1->pPrec=object2->pPrec;
object1->pNext=object2->pNext;
object2->pPrec=temp1->pPrec;
object2->pNext=temp1->pNext;

free(temp1);
}

When I traverse the list for the 2nd time,i get: M4,M3,M1. 当我第二次遍历列表时,我得到:M4,M3,M1。 Also the dim of the list is now 3 instead of 4,M2 magically disappearing. 而且,列表的暗淡现在是3,而不是4,M2消失了。

Can anyone provide me an explanation on why I am getting this and what I am doing wrong? 谁能为我提供解释为什么我得到了这个消息以及我做错了什么?

Thank you in advance. 先感谢您。

This is much easier if you draw it on paper and research it that way. 如果您将其绘制在纸上并以这种方式进行研究,这会容易得多。

Assuming object1 and object2 are not NULL. 假设object1和object2不为NULL。

You need to think about the nodes that come before and after the two nodes you are swapping. 您需要考虑要交换的两个节点之前和之后的节点。 The node just before your two swap nodes needs to point to object2 now instead of object1 (assuming that the node exists). 两个交换节点之前的节点现在需要指向object2而不是object1(假设该节点存在)。 And you need to make object2's preceding now point to the node just before your two swap nodes. 现在,您需要使object2的前面指向两个交换节点之前的节点。

if ( object1->pPrec != NULL )
   object1->pPrec->pNext = object2;
object2->pPrec = object1->pPrec;

And you need to make object1's next points to the node just after your two swap nodes and that node needs to point back to object1 (if it exists). 并且,您需要两个交换节点之后使object1的下一个指向该节点,并且该节点需要指向object1(如果存在)。

object1->pNext = object2->pNext;
if ( object2->pNext != NULL ) 
    object2->pNext->pPrec = object1;

Then object1 needs to point back to object2 and object2 needs to point forward to object1. 然后,object1需要指向对象2,而object2需要指向对象1。

object1->pPrec = object2;
object2->pNext = object1;

The problem statement doesn't specify what points to the first node of the list (M4), or if there is a pointer to the last node of the list (M1). 问题语句未指定指向列表的第一个节点(M4)的指针,也没有指定指向列表的最后一个节点(M1)的指针。 It doesn't state if the list is circular, so I assume it's not circular. 它没有说明列表是否是循环的,所以我认为它不是循环的。

When swapping nodes in a list, if the nodes to be swapped are adjacent (which is the example case, M3 and M2), then pointers need to be "rotated". 交换列表中的节点时,如果要交换的节点是相邻的(在本例中为M3和M2),则指针需要“旋转”。 If the nodes to be swapped are not adjacent, then pointers need to be swapped. 如果要交换的节点不相邻,则需要交换指针。 The same logic can be used for both cases by first swapping what ever points to the two nodes, then swapping the two nodes pointers. 通过首先交换指向两个节点的指针,然后交换两个节点的指针,可以在两种情况下使用相同的逻辑。

Using pointer to pointer to node will simplify the logic. 使用指向节点的指针将简化逻辑。

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

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