使用链表在C中进行插入排序 [英] Insertion sort in C using linked list
问题描述
我必须制作一个电话簿程序.该程序应从文件中读取名称和编号.我已经成功创建了一个包含此数据的链表.现在,我想按字母顺序对它们进行排序.我该怎么办?
I have to make a telephone directory program. The program should read names and numbers from a file. I have successfully created a linked list containing this data. Now I want to sort them alphabetically. How should I do that?
推荐答案
这取决于您的目标.
如果要高效地执行此操作,请将指向每个元素的指针插入数组中,然后使用类似快速排序(C中的qsort
);最后,从排序后的数组中重新创建列表.
If you want to do this efficiently, stick pointers to every element into an array, and then sort the array alphabetically using an algorithm like quicksort (qsort
in C); lastly, re-create the list from the sorted array.
另一方面,如果这是家庭作业,并且您必须按照帖子标题的说明使用插入排序,那就是另一回事了.
On the other hand, if this is homework and you have to use insertion sort as the title of the post suggests, it's a different matter.
这篇关于使用链表在C中进行插入排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!