包含指向下一个的指针的排序结构
Sort struct that contains pointer to the next
所以我有这样的东西:
struct Something
{
int number;
Something* next;
};
我想按数字对它们进行排序,虽然我不想更改数字,但我想更改指向下一个的指针。
我该怎么做?
我知道"列表"的结束和开始,(先进先出顺序)
对链表使用 MergeSort:首先使用两个正在运行的指针遍历列表,其中一个指针前进速度慢两倍。当您到达列表末尾时,慢指针指向中间。拆分列表并递归地对两半进行排序,然后合并。
有各种排序算法(http://www.sorting-algorithms.com/),到目前为止,我会在这个例子中使用random|selection算法。它将看起来像这样:
void Something::SortAscending() {
Something* current = next;
Something* smallest = next;
while( current ) {
if( current->number < smallest->number ) {
smallest = current;
}
current = current->next;
}
if( smallest != next )
smallest->next = next;
next = smallest;
next->SortAscending();
}
但是请注意,如果您被赋予列表的头部,则可能不会使用我上面提供的功能类型对其进行更改。
排序 -> 对于排序数组的每个元素更新下一个指针
相关文章:
- 将结构向量排序为子组
- 带有枚举方向/类型的气泡排序结构数组
- C++对结构向量进行排序
- 在C++中对结构向量进行排序时出现问题
- 按字母顺序对结构内数组变量中的名称进行排序
- 保持排序的数据结构,允许log N插入时间,并且可以返回我在log N中查找的元素的索引
- 使用选择排序对数组数据结构进行排序,但它不起作用
- 对结构向量进行两次排序
- C++使用QuickSort对数组结构进行排序
- 如何选择排序一些结构
- 对结构int成员进行排序
- 在排序链表中插入结构并将其写入二进制文件
- 对坐标结构的向量进行排序
- C++ 中优先级队列的结构排序条件
- 按姓氏合并排序结构,导致分段错误
- C++ 无效的比较器排序结构向量
- 排序结构的队列
- C++排序结构链表
- 包含指向下一个的指针的排序结构
- 用STL算法排序结构的动态数组