C++单链表选择排序
来源:互联网 发布:上海民族一厂淘宝官网 编辑:程序博客网 时间:2024/06/05 01:17
#include <iostream>
struct Node {
int value;
Node *next;
Node(int data) : value(data), next(nullptr) {};
};
Node * ListSelectSort(Node * head);
Node * getSmallestNode(Node * head);
void print(Node * head);
int main()
{
Node n1(15);
Node n2(3);
Node n3(8);
Node n4(11);
Node n5(2);
Node n6(1);
n1.next = &n2;
n2.next = &n3;
n3.next = &n4;
n4.next = &n5;
n5.next = &n6;
print(ListSelectSort(&n1));
system("pause");
return 0;
}
Node * ListSelectSort(Node * head)
{
Node * tail = nullptr;
Node * cur = head;
Node * smallpre = nullptr;
Node * small = nullptr;
while (cur != nullptr)
{
small = cur;
smallpre = getSmallestNode(cur);
if (smallpre != nullptr)
{
small = smallpre->next;
smallpre->next = small->next;
}
cur = cur == small ? cur->next : cur;
if (tail == nullptr)
{
head = small;
}
else
{
tail->next = small;
}
tail = small;
}
return head;
}
Node * getSmallestNode(Node * head)
{
Node * smallpre = nullptr;
Node * pre = head;
Node * small = head;
Node * cur = head->next;
while (cur != nullptr)
{
if (cur->value < small->value)
{
smallpre = pre;
small = cur;
}
pre = cur;
cur = cur->next;
}
return smallpre;
}
void print(Node * head)
{
while (head != nullptr)
{
std::cout << head->value << std::endl;
head = head->next;
}
}
struct Node {
int value;
Node *next;
Node(int data) : value(data), next(nullptr) {};
};
Node * ListSelectSort(Node * head);
Node * getSmallestNode(Node * head);
void print(Node * head);
int main()
{
Node n1(15);
Node n2(3);
Node n3(8);
Node n4(11);
Node n5(2);
Node n6(1);
n1.next = &n2;
n2.next = &n3;
n3.next = &n4;
n4.next = &n5;
n5.next = &n6;
print(ListSelectSort(&n1));
system("pause");
return 0;
}
Node * ListSelectSort(Node * head)
{
Node * tail = nullptr;
Node * cur = head;
Node * smallpre = nullptr;
Node * small = nullptr;
while (cur != nullptr)
{
small = cur;
smallpre = getSmallestNode(cur);
if (smallpre != nullptr)
{
small = smallpre->next;
smallpre->next = small->next;
}
cur = cur == small ? cur->next : cur;
if (tail == nullptr)
{
head = small;
}
else
{
tail->next = small;
}
tail = small;
}
return head;
}
Node * getSmallestNode(Node * head)
{
Node * smallpre = nullptr;
Node * pre = head;
Node * small = head;
Node * cur = head->next;
while (cur != nullptr)
{
if (cur->value < small->value)
{
smallpre = pre;
small = cur;
}
pre = cur;
cur = cur->next;
}
return smallpre;
}
void print(Node * head)
{
while (head != nullptr)
{
std::cout << head->value << std::endl;
head = head->next;
}
}
阅读全文
0 0
- c语言 单链表 选择排序+归并排序
- C实现单链表直接选择排序
- C语言选择排序
- 选择排序 C
- C算法-选择排序
- c语言选择排序
- 选择排序C实现
- c语言选择排序
- C语言---选择排序
- C语言 选择排序
- 选择排序(C语言版)
- c 选择排序
- c语言选择排序
- C实现选择排序
- [c++]选择排序
- 【C语言】选择排序
- linux c选择排序
- C语言:选择排序
- C++抛出异常
- 数据库关系代数表达式
- Scrapy使用简记
- ARM内连汇编_asm_
- 基于Node.js的微信JS-SDK后端接口实现
- C++单链表选择排序
- 接入层负载均衡技术-lvs为何不能完全替代DNS轮询
- Enum --枚举妙用
- 学习笔记 AVL树
- IE下 jquery的fadeIn与fadeOut方法失效的BUG
- 1110. Complete Binary Tree (25)
- 度度熊回家 (模拟)
- 多线程阻塞类型
- Codeforces Round #428