在不使用临时变量的情况下在两个节点之间插入节点 [英] Inserting a node in between two nodes without using temporary variable
问题描述
Hello Guys,
有一个链接列表,要求是在节点A和节点B之间插入一个节点C而不使用任何临时变量。
如何在不使用任何临时变量的情况下完成这项工作?
我认为我们可以这样做如果我错了,请告诉我。
假设链接结构如下所示。
Hello Guys,
There is a single link list, The requirement is to insert one node C in between node A and Node B without using any temporary variable.
How can this be done without using any temporary variable?
I think we can do this as below, please let me know If I am wrong.
Assuming that link structure is as given below.
Struct Node
{
int a;
Struct Node * next
}
步骤1-首先,我将创建如下的节点C.
Step 1- First, I Will create a Node C as below.
Struct Node * C = (Struct Node *)malloc(sizeof(Struct Node));
步骤2-由于我需要在节点A和B之间插入此节点,我将按如下方式进行操作。
Step-2- As I need to insert this node in between node A and B, I will do as below.
C->next = A->next;
(现在,C开始指向节点B因为,A-> next具有节点B的地址)
( Now, C started pointing to node B because, A->next has address of Node B)
A-> next = C;
(现在,A开始指向节点C)
我可能错了,请指正。
问候,
Joy
( Now, A started pointing to node C)
I may be wrong, please correct me.
Regards,
Joy
推荐答案
你还需要将C链接到链中AB。
C-> next = B;
结果是ACB
you also need to link C into the chain of A-B.
C->next = B;
The result is A-C-B
这篇关于在不使用临时变量的情况下在两个节点之间插入节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!