20170515_单链表的一种新颖排序算法—借助list容器
来源:互联网 发布:地理空间数据云 编辑:程序博客网 时间:2024/06/08 13:33
20170515_单链表的一种新颖排序算法—借助list容器
//147. Insertion Sort List //Sort a linked list using insertion sort.//148. 是Sort List,使用的是归并排序法。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */#include<iostream>#include<vector>#include<list>using namespace std;struct ListNode{int val;ListNode *next;ListNode(int x):val(x),next(nullptr) {}};ListNode *CreatList(ListNode * &root, vector<int> &a){int n=a.size();if(n==0)return root=nullptr;else{root=new ListNode(a[0]);ListNode *p=root;for(int i=1; i<n; ++i){ListNode *r=new ListNode(a[i]);p->next=r;p=r;}return root;}}void OutList(ListNode *root){ListNode *p=root;if(p==nullptr)return;else{while(p != nullptr){if(p->next != nullptr)cout<<p->val<<",";elsecout<<p->val<<endl;p=p->next;}}}class Solution{public: ListNode *insertionSortList(ListNode *head){ ListNode *root=head;if(root == nullptr || root->next == nullptr)return root;else{list<int> ilist;for(ListNode *p=root; p!=nullptr; p=p->next)ilist.push_back(p->val);ilist.sort();//升序排列for(ListNode *p=root; p!=nullptr; p=p->next){p->val=ilist.front();//取出list的第一个元素放入单链表中ilist.pop_front();//删除list 中的第一个元素}return root;} }};int main(){vector<int> a;int ch;cout<<"依次输入单链表节点数据:"<<endl;while(cin>>ch)a.push_back(ch);cin.clear();cin.sync();ListNode *r;ListNode *root=CreatList(r,a);cout<<"单链表建立完成."<<endl;cout<<"顺序输出单链表:"<<endl;OutList(root);cout<<"顺序输出单链表完成."<<endl<<endl;Solution example;ListNode *result;result=example.insertionSortList(root);OutList(root);cout<<"顺序输出单链表完成."<<endl<<endl;system("pause");return 0;}
阅读全文
0 0
- 20170515_单链表的一种新颖排序算法—借助list容器
- 20170515_容器list的函数
- 一种新颖的图像分割方法
- 一种新颖的智能手机助理软件
- list容器的特殊sort()算法:非递归--归并排序
- STL源码剖析——list容器的排序算法sort()
- STL源码剖析——list容器的排序算法sort()
- 单链表希尔排序算法的一种实现
- 偶尔看到的一种新颖的流程控制方式
- 一种新的排序算法
- List的排序算法
- List排序(较实用的一种)
- 模板_一种排序
- list 容器 排序函数
- list容器排序问题?
- list 容器 排序函数
- 数组归并排序算法的一种实现
- 快速排序算法的一种实现
- js判断滚动条滚动方向
- Angular开发(十五)-关于父子组件中投影的介绍
- pytho爬虫中文乱码解决方法
- java ObjectMapper
- 再生核希尔伯特空间(RKHS)和核函数
- 20170515_单链表的一种新颖排序算法—借助list容器
- 对数据结构一点一小小的理解(六)——队列Queues
- 【English】No Failure
- 楼主的文章列表
- ThinkPHP3.2.3--登录与退出登录
- 2016年蓝桥杯决赛 凑平方数
- git 分支改名
- Http4.3工具类(连接池和非连接池实现)
- 在VMware 12 Pro上安装Mac OS 虚拟机