冒泡排序链表,交换指针。 C
我正在尝试使用冒泡排序交换单链表的两个指针。 我已经制作了比较功能,并且运行良好。 在交换函数中,交换运行良好,我已经成功地在节点和 node->next
之间交换,尽管链表“丢失”了节点的信息(在swap),所以链表中的第一个节点是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;
}
I'm trying to swap two pointers of a singly linked list using the bubble sort.
I've made the compare function, and its working good.
In the swap function, the swap is working good, I've managed to swap between the node and the node->next
, though the linked list "lose" the info of the node (after the swap), so the first node in the linked list is node->next
.
I'm using a generic function which do the bubble sort and call the compare function and the swap function.
Any idea why this happens?
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要交换两个节点 (a,b),您需要访问指向第一个节点的“外部”节点 (o)。 (a 和 b 之后还有一个节点 p。(p 也可以为 NULL,但这并不重要)
旧情况:
新情况:
只有当 o 实际上 < 时,才能执行此交换 /em> 一个节点(因此:有一个 o->next 指针),因此您需要特殊的代码来处理 a 是链头的情况,
但不是:o->。 ;next 只是一个“struct llist *”,所以任何在大多数情况下,最简单的解决方案是使用交换函数的指针到指针参数;指针到指针可以指向链的头,也可以指向某个节点的指针。 -> 下一个指针。
For swapping two nodes (a,b), you need access to an "outside" node (o), which points to the first. (and there is also a node p after a and b. (p could be NULL, too, but that is not important)
Old situation:
New situation:
This swap can only be performed if o actually is a node. (and thus: has an o->next pointer), so you'll need special code to handle the case where a is the head of the chain.
But no: o->next is only a "struct llist *", so any pointer to a llist can be used. In most cases, the simplest solution is to use a pointer-to-pointer argument for the swap function; a pointer-to-pointer can either point to the head of the chain, or to some node's ->next pointer.
我还尝试使用冒泡排序来实现链表的排序,尽管您可以在代码中进行的修改不是编写交换函数,您只需在代码中交换它们,每次您都需要有 3 个不同的指针指向相邻节点。您可以在此处找到已实现的代码。
I was also trying to implement sorting of linkedlist using bubble sort, though the modification you can do in your code would be rather than writing a swap function you could just swap them in your code only and each time you would need to have 3 different pointers pointing to adjacent nodes. You can find the implemented code here.