当我被告知“对单链表进行排序”时,预期会发生什么?
它可能是出于迂腐的目的,但不是家庭作业。我有以下关于“对单链表进行排序”(任何类型的列表)的问题,假设对于这个问题,我们希望按每个节点中的值的升序进行排序。
这个问题是否期望我通过交换列表中每个节点的值来对列表进行排序,以便这些值按某种顺序(升序/降序)。这里节点的原始值(排序之前)将被更改以反映排序后的列表。这里返回相同的早期头指针,但现在其中包含最小的元素,这可能与其早期元素不同。
就像我下面的C代码一样(下面的代码可能无法按原样编译,但我可以正常工作。在这里发布作为说明以澄清我的观点):
struct lnk_lst
{
int val;
struct lnk_lst *next;
};
main()
{
//Assume curr is the head node of the singly linked list created with some values
curr = llist_bubble_sort(curr);
}
struct lnk_lst* llist_bubble_sort(struct lnk_lst *lst)
{
int i,n=0,tmpint;
struct lnk_lst *tmp=lst,*tmp2=lst;
while(tmp->next)
{
lst = tmp2;
while(lst->next)
{
if(lst->val > lst->next->val)
{
tmpint = lst->val;
lst->val = lst->next->val;
lst->next->val = tmpint;
}
lst = lst->next;
}
tmp = tmp->next;
}
return tmp2;
}
或者
是否期望指向具有最小元素的节点的指针(假设升序)原始排序被移动为新的头节点,然后具有下一个最小元素的节点链接到头节点,依此类推,使得列表完全重新排序,现在返回的头节点不是同一个指针和之前一样。
如果列表排序的解释是这一秒,那么我必须看看如何在代码中实现这个想法。
It might be for pedantic purposes but not homework. I have below question about 'Sorting a singly linked list' (Any kind of list for that matter) Assume for this questions we want to sort in ascending order of values in each node.
Does this question expect me to just sort the list by swapping the values of each node in the list so that the values are in some order(ascending/descending). Here the original values of nodes (before sorting) would be changed in order to reflect the sorted list. Here the same earlier head pointer is returned but now having the smallest element in it, which might be different than its earlier element.
Like this C code I have below(The code below might not compile as is , but I have it working fine. Posted here as illustrative to clear my point):
struct lnk_lst
{
int val;
struct lnk_lst *next;
};
main()
{
//Assume curr is the head node of the singly linked list created with some values
curr = llist_bubble_sort(curr);
}
struct lnk_lst* llist_bubble_sort(struct lnk_lst *lst)
{
int i,n=0,tmpint;
struct lnk_lst *tmp=lst,*tmp2=lst;
while(tmp->next)
{
lst = tmp2;
while(lst->next)
{
if(lst->val > lst->next->val)
{
tmpint = lst->val;
lst->val = lst->next->val;
lst->next->val = tmpint;
}
lst = lst->next;
}
tmp = tmp->next;
}
return tmp2;
}
OR
Is it expected to that the pointer to the node with the smallest element(assuming ascending order) in the original ordering is moved as new head node, then the node having the next smallest element is linked to the head node, and so on such that the list is reordered completely and now the head node returned is not same pointer as earlier.
If the interpretation of list sort is this second, then I have to see how yet get the idea done in code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
链接列表背后的整个想法是可以更改链接而不影响内容。更改指针有时涉及创建指向指针变量的指针。另外:如果您只交换内容,您也可以省略 ->next 指针,而是使用数组,然后交换数组的内容。
恕我直言,对链表进行排序的自然方式是合并排序。下面的片段将列表分为两部分:已经到位的节点和尚未到位的节点。对第二个列表进行排序,并将两个列表合并。拆分和合并都涉及一些指针到指针变量。
结果:
The whole idea behind linked lists is that the links can be changed without affecting the content. Changing a pointer sometimes involves creating a pointer to pointer variable. Also: if you only swap the contents, you could just as well leave out the ->next pointers, use an array instead, and swap the array's contents.
IMHO the natural way of sorting a linked list is mergesort. The fragment below splits the list in two part: the nodes that are already in place, and those that are not. The second list is sorted, and the two lists are merged. Both splitting&merging involve some pointer-to-pointer variables.
RESULT:
对链表进行排序的正确方法是第二种。这也是有道理的,因为您通常希望根据某些属性对对象进行排序,同时保留其其他属性值。在这种情况下,第一种方法并没有多大帮助。
这个想法类似于传统的排序方法。例如,您可以通过迭代元素然后将下一个元素插入到正确排序的链表中的正确位置直到该元素来使用插入排序。有关 C 语言的详细代码,您可以在此处查看维基百科页面。
The correct way of sorting a linked list is the second one. This also makes sense because often you would want to sort objects on some attribute while retaining their other attribute values. In that case, the first approach doesn't help much.
The idea is similar to traditional sorting approaches. For example you can use insertion sort by iterating through the elements and then inserting the next element in the right position in the correctly sorted linked list upto that element. For detailed code in C, you can see the Wikipedia page here.