I need to merge two doubly-linked lists, but not by their values (the lists are not sorted). I want to obtain a single list that has all the nodes from the two, but in the order in which they appear in memory.
我需要合并两个双向链表,但不是它们的值(列表没有排序)。我想获得一个列表,其中包含两个节点中的所有节点,但按照它们在内存中的显示顺序。
Maybe this image helps more: http://img140.imageshack.us/i/drawing2.png/
也许这个形象有助于更多:http://img140.imageshack.us/i/drawing2.png/
Is there any algorithm (preferably a fast one) that can do this kind of merge ? Maybe this is a bit helpful:
有没有可以进行这种合并的算法(最好是快速算法)?也许这有点帮助:
- the start node of the lists is always before the other ones.
- a list can have up to 8192 nodes.
- I know where the nodes are located in memory because the lists keep track of free location in a big block of memory (used in a memory allocator).
- I work in C++.
列表的起始节点总是在其他节点之前。
一个列表最多可以有8192个节点。
我知道节点在内存中的位置,因为列表跟踪大块内存中的空闲位置(在内存分配器中使用)。
我在C ++工作。
Thanks in advance!
提前致谢!
2 个解决方案
#1
Well it sounds like you aren't using std::list
, so I'll go with the generic solution. Since your requirement is to merge the lists, but have the nodes be in order of there physical location in memory. You can likely just append the two lists, then sort the nodes by address of the nodes.
好吧,听起来你没有使用std :: list,所以我会选择通用解决方案。由于您的要求是合并列表,但是节点的顺序是内存中的物理位置。您可以只追加两个列表,然后按节点的地址对节点进行排序。
see: http://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.html for a sorting algorithm for linked lists.
请参阅:http://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.html,了解链接列表的排序算法。
When sorting, instead of comparing node1->value < node2->value
just do (size_t)node1 < (size_t)node2
, or something to that nature.
在排序时,不是比较node1-> value
#2
"Merge" is a bit misleading here, as you can only merge sorted lists, and yours aren't sorted.
“合并”在这里有点误导,因为你只能合并排序列表,而你的列表没有排序。
You need to sort, not to merge.
你需要排序,而不是合并。
Put the pointers to nodes into a vector. use std::sort on the vector, and pass a comparison functor that casts each pointer to size_t (i think) and compares the resulting numbers.
将节点指针放入向量中。在向量上使用std :: sort,并传递一个比较函子,将每个指针强制转换为size_t(我认为)并比较结果数字。
You can then rearrange your linked lists according to the resulting order in the vector.
然后,您可以根据向量中的结果顺序重新排列链接列表。
#1
Well it sounds like you aren't using std::list
, so I'll go with the generic solution. Since your requirement is to merge the lists, but have the nodes be in order of there physical location in memory. You can likely just append the two lists, then sort the nodes by address of the nodes.
好吧,听起来你没有使用std :: list,所以我会选择通用解决方案。由于您的要求是合并列表,但是节点的顺序是内存中的物理位置。您可以只追加两个列表,然后按节点的地址对节点进行排序。
see: http://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.html for a sorting algorithm for linked lists.
请参阅:http://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.html,了解链接列表的排序算法。
When sorting, instead of comparing node1->value < node2->value
just do (size_t)node1 < (size_t)node2
, or something to that nature.
在排序时,不是比较node1-> value
#2
"Merge" is a bit misleading here, as you can only merge sorted lists, and yours aren't sorted.
“合并”在这里有点误导,因为你只能合并排序列表,而你的列表没有排序。
You need to sort, not to merge.
你需要排序,而不是合并。
Put the pointers to nodes into a vector. use std::sort on the vector, and pass a comparison functor that casts each pointer to size_t (i think) and compares the resulting numbers.
将节点指针放入向量中。在向量上使用std :: sort,并传递一个比较函子,将每个指针强制转换为size_t(我认为)并比较结果数字。
You can then rearrange your linked lists according to the resulting order in the vector.
然后,您可以根据向量中的结果顺序重新排列链接列表。