我正在编写一个冒泡排序函数来对双向链表中的节点进行排序。我的功能非常接近工作,但我缺少一些简单的东西,我无法弄清楚。
void sort (struct lnode** head, void (*swapPtr) (struct lnode** head, struct lnode* n1, struct lnode* n2),
int(*comparePtr) (void* v1, void* v2)) {
struct lnode* next;
struct lnode* temp = *head;
int comp;
struct lnode* temp2;
int count = 0;
while (temp != NULL) {
temp2 = nodeGetNext(temp);
temp = temp2;
count++;
}
temp = *head;
for(int i = 0; i < count; i++) {
next = nodeGetNext(temp);
comp = comparePtr(temp,next);
if (comp == 1)
swapPtr(head, temp, next);
else if (comp == -1)
swapPtr(head, next, temp);
temp = nodeGetNext(next);
}
}
当我运行函数时,它只交换前两个节点。我猜我没有在 for
循环结束时正确设置温度。我尝试了几种不同的方法,但没有取得任何成功。如果有任何帮助,我将不胜感激!
最佳答案
关于c - 使用链表进行冒泡排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12811249/