【发布时间】:2012-02-17 13:34:24
【问题描述】:
我正在尝试使用冒泡排序交换单链表的两个指针。
我已经制作了比较功能,并且效果很好。
在交换函数中,交换运行良好,我设法在节点和node->next 之间进行交换,虽然链表“丢失”了节点的信息(交换之后),所以第一个节点在链表是node->next。
我正在使用一个通用函数来进行冒泡排序并调用比较函数和交换函数。
知道为什么会这样吗?
void swap_arr(void **arr,int i , int j)
{
Team *teamList = (Team*) arr ;
Team *teamI = (Team*) arr , *teamJ ;
Team *temp ;
Team *temp1;
int z;
// Receives instead i
for(z=0; z<i; z++)
teamI = teamI->next;
//teamJ is the i+1
teamJ = teamI->next;
temp = teamI;
temp1 = teamJ->next;
teamI = teamJ ;
teamJ = temp;
if (temp1->next->next==NULL)
teamJ->next = NULL;
else
teamJ->next = temp1->next;
teamI->next = teamJ;
if (temp1==NULL)
teamJ->next=NULL;
else
teamJ->next = temp1;
}
【问题讨论】:
-
这个问题可以通过调试器解决。
-
j参数有什么用? -
j=i+1,我需要交换的第二个节点的“地方”。 havnt 确实在代码中找到了它的用途,但由于硬件说明,我不得不将其保留在这里
-
我尝试过使用调试器,尽管我尝试的每一件事都给了我同样的问题。
标签: c pointers swap bubble-sort singly-linked-list