为什么是无限循环?
无限循环是因为列表中的自循环after呼叫swap()
功能。在swap()
语句后面的代码有错误。
(*b)->next = (temp1)->next;
Why?:因为在赋值语句之后swap()
功能temp1
的下一个开始指向b
节点。和node[b]
循环中的下一个指向自身。还有自循环是因为无限循环,在代码中遍历链表的某个位置。
下面我画了图来展示如何swap()
逐步进行。也许这可以帮助您了解您的error:
你没有提到,但我假设链表之间有以下关系a
and b
: (阅读红色评论)
(步骤1):
+----+----+----+ +---+----+----+
| one |----->| two |
+----+----+----+ +---+---+-----+
^ ^ ^ ^
| | | |
| *a | *b
| |
temp1 temp2, temp3 "after assignment to temp variables"
(step-2): ^
|
*a = *b | *a "<--- next step"
(步骤 3):有缺陷的声明
(*b)->next = (temp1)->next; "Change link: (temp1)->next; is `two` node"
" *b is `two`, So Self loop"
+----+----+----+ +---+----+----+ <---|
| one | | two |-----|
+----+----+----+ +---+---+-----+
^ ^ ^ ^
| | | |
| | *b *a
| |
temp1 temp2, temp3 " after assignment to temp"
See (temp1)->next;
实际上是b
你正在分配(*b)->next = (*b)
通过做(*b)->next = (temp1)->next;
因此添加一个自循环。
(步骤4):
我认为通过图表您可以轻松理解最后两行swap()
代码正在做:
temp2 = temp1;
(temp2)->next = temp3->next;
以下是我对这两行的图表:
temp2 = temp1;
+----+----+----+ +---+----+----+ <---|
| one | | two |-----| "<--- Self loop"
+----+----+----+ +---+---+-----+
^ ^ ^ ^
| | | |
| | *b *a
| |
temp2 = temp1; temp3
(步骤 5):即使是函数的最后一行swap()
左循环如下:
(temp2)->next = temp3->next; " last line of your code"
+----+----+----+ +---+----+----+ <---|
| one |----->| two |-----| "<-- Self loop"
+----+----+----+ +---+---+-----+
^ ^ ^ ^
| | | |
| | *b *a
| |
temp2 = temp1; temp3
所以循环仍然存在于two
节点如此无限循环。
如何交换单链表中的两个节点?
一种方法是交换节点的数据,而不是交换节点在链表中的位置(正如我对你的问题的评论). But 你想交换节点的在列表中的位置。
嗯,这很好!如果节点数据量较大,此时最好交换节点的位置而不是交换节点的数据(交换数据将是糟糕的选择)
因为你有单链表,交换列表中的任意两个节点need there 前一个节点地址 too. (这是你在交换逻辑中没有考虑的一点)
为什么需要先前的指针?:
假设在一些成功的插入(推送)操作之后,您的列表变为如下:
0 <--------TOP - "head"
9 <--p
2
6 <--q
5
水平图 - 假设你想交换说两个节点(q)
and (p)
:
+---+ +---+ +---+ +---+ +---+
| 0 |--->| 9 |--->| 2 |--->| 6 |--->| 5 |---
+---+ +---+ +---+ +---+ +---+ |
^ ^ ^ null
| | |
| (q) (p)
(head)
正如我所说,要交换我们需要先前的指针。你需要考虑以下
(理论上,我正在为特定节点编写(p)
and (q)
只是为了让解释简单。但我的实现很一般):
在列表先前的指针中:
node[0] points to node[9] that is (q), and
node[2] points to node[6] that is (p)
和
node[9] points to node[2]
node[6] points to node[5]
NOTICE:如果你想交换两个节点说node[ 9 ]
and node[ 6 ]
那么你应该使用这两个节点之前的节点的指针。
例如:两次交换node[ 9 ]
and [ 6 ]
,您还需要更改下一个指针node[ 0 ]
和下一个指针node[ 2 ]
在上图中。
交换这两个节点后列表会怎样?
+---+ +---+ +---+ +---+ +---+
| 0 |--->| 6 |--->| 2 |--->| 9 |--->| 5 |---
+---+ +---+ +---+ +---+ +---+ |
^ ^ ^ null
| | |
| (p) (q)
(head)
之前的节点现在是什么[o]
and [2]
?
交换后,列出先前的指针
node[0] points to node[6] that is (q), and
node[2] points to node[9] that is (p)
And
node[9] points to node[5]
node[6] points to node[2]
所以如果你想交换两个节点;紧邻的前一个节点也会起作用,并且因为列表是单链接列表,所以您也需要前一个指针。
如何找到之前的节点指针?
假设你想交换任意两个节点node[p]
and node[q]
那么你可以使用head pointer
找到前一个节点。
所以交换功能syntax (在我的实现中) 就好像:
void swap(struct stack **head, // head node
struct stack **a, // first candidate node to swap
struct stack **b); // first candidate node to swap
你将调用如下函数:
swap(&head, &p, &q);
定义: (要理解代码,请阅读我在几乎每一行添加的注释)
void swap(struct stack **head,
struct stack **a,
struct stack **b){
// first check if a agrgument is null
if( (*head) == NULL || // Empty list
(*a) == NULL || (*b) == NULL){ // one node is null
// Nothing to swap, just return
printf("\n Nothing to swap, just return \n");
return;
}
// find previos nodes
struct stack* pre_a = get_prevnd(*head, *a);
struct stack* pre_b = get_prevnd(*head, *b);
//Now swap previous node's next
if(pre_a) pre_a->next = (*b); // a's previous become b's previous, and
if(pre_b) pre_b->next = (*a); // b's previous become a's previous
//Now swap next fiels of candidate nodes
struct stack* temp = NULL;
temp = (*a)->next;
(*a)->next = (*b)->next;
(*b)->next = temp;
//change head: if any node was a head
if((*head)==(*a))
*head = *b;
else
if((*head)==(*b))
*head = *a;
}
In swap()
你可以注意到我调用了一个辅助函数get_prevnd(, );
。该函数返回列表中前一个节点的地址。在函数中get_prevnd(, );
,第一个参数是列表头,第二个参数是您要查找的节点。
// find previous node function()
struct stack* get_prevnd(
struct stack* head,
struct stack* a
){
if(head == a){
// node[a] is first node
return NULL;
}
struct stack* temp = head; // temp is current node
struct stack* pre_a = NULL;
while(temp && temp!=a){ //search while not reach to end or the node
pre_a = temp; // find previous node
temp = temp->next;
}
if(temp!=a){// node[a] not present in list
fprintf(stderr, "\n error: node not found!\n");
exit(EXIT_FAILURE); // bad technique to exit()
}
return pre_a;
}
幸运的是,代码可以工作:)。以下是此代码的在线测试链接。我已经测试了各种输入。
键盘:交换单链表中的节点。 http://codepad.org/3YaGfYxm请检查输出。
And sorry for bad English