解题思路:

排序方法:多路归并排序

每次将n个list的头元素取出来,进行排序(堆排序),最小元素从堆中取出后,将其所在list的下一个元素

放入堆中,调整堆序列。

函数实现原型:

void listnodesort(list<list<Node> >* listlistnode){}

#include <iostream>
#include <list> using namespace std; struct Node{
int value;
Node *next = NULL;
};

///堆排序(调整堆)
void HeapAdjust(Node* heap, int i, int length){
int min = i;
int lch = *i+;
int rch = *i+; if(i <= (length-)/){
if(lch < length && heap[min].value > heap[lch].value){
min = lch;
}
if(rch < length && heap[min].value > heap[rch].value){
min = rch;
}
if(min != i){
swap(heap[min], heap[i]);
HeapAdjust(heap, min, length);
}
}
}
///堆排序(建堆)
void BuildHeap(Node* heap, int length)
{
   ///要确定好是-2,还是-1
for(int i = (length-)/; i >=; i--){
HeapAdjust(heap, i, length);
}
}

///主要排序函数
void listnodesort(list<list<Node> >* listlistnode)
{
list<Node> listTotal;
int length = (*listlistnode).size();
Node* heap = new Node[length];
int i = ;
for(list<list<Node> >::iterator iter = (*listlistnode).begin(); iter !=(*listlistnode).end(); iter ++){
heap[i] = (*iter).front();
i++;
}
BuildHeap(heap, length);
while(length>){
listTotal.push_back(heap[]);
if(length == ){
break;
}
if(heap[].next == NULL){
heap[] = heap[length-];
length -=;
}
else{
heap[] = *(heap[].next);
}
HeapAdjust(heap, , length);
}
cout << "totalList: ";
for(list<Node>::iterator it=listTotal.begin(); it!=listTotal.end(); it++){
cout << (*it).value << " ";
}
cout << endl;
} list<list<Node> > buildListList(int sum, int interval)
{
list<list<Node> > listlistnode;
for(int i = ; i<interval; i++){
list<Node> listnode; ///mode 1: start
Node* node = new Node(); ///此处一定要用new分配一个内存,不能直接使用Node node;
int j = i;
while(j<sum){
Node* nodeNext = new Node();
node->value = j;
if(j+interval > sum){
node->next = NULL;
}
else
node->next = nodeNext;
listnode.push_back(*node);
node = nodeNext;
j+=interval;
}
///mode 1: end ///mode2:start
// int j = i;
// Node* head = NULL;
// Node* endp = NULL;
// while(j<sum){
// Node* node = new Node();
// node->value = j;
// node->next = NULL;
// if(head == NULL){
// head = node;
// endp = node;
// }
// else{
// endp->next = node;
// endp = node; ///此处也不可忘了
// }
// j=j+interval;
// }
// while(head != NULL){
// listnode.push_back(*head);
// head = head->next;
// }
/// mode 2:end listlistnode.push_back(listnode);
} return listlistnode;
} void print(list<list<Node> > *listlistnode) ///到底是传值还是传指针,后续代码块中的操作也要做相应变动;
{
for(list<list<Node> >::iterator iter=(*listlistnode).begin(); iter != (*listlistnode).end(); iter++){
list<Node> listnode = *iter; ///迭代器解地址后就是其内部所指内容
for(list<Node>::iterator it=listnode.begin(); it!=listnode.end(); it++){
cout << (*it).value << " ";
}
cout << endl;
}
} int main()
{
list<list<Node> > listlistnode = buildListList(,);
print(&listlistnode);
listnodesort(&listlistnode); ///根据函数的定义决定是传值还是传指针 cout << "Hello world!" << endl;
return ;
}

另外,还有一种更加简单的方法:借助map结构,因为map本身就是key有序的序列结构,所以将listlistnode中的所有元素依次加入map中即可完成排序。

   map<int, int> mapNode;
for(list<list<Node> >::iterator iter=listlistnode.begin(); iter != listlistnode.end(); iter++){
list<Node> listnode = *iter;
for(list<Node>::iterator it=listnode.begin(); it!=listnode.end(); it++){
        ///如果不存在则插入,如果存在则key相应得value++即可。
if(mapNode.count((*it).value)==)
mapNode.insert(pair<int, int> ((*it).value,));
else{
mapNode[(*it).value]++;
}
}
} for(map<int, int>::iterator it = mapNode.begin(); it!=mapNode.end(); it++){
cout << it->first << " " << it->second << endl;
}

最新文章

  1. C#创建dll类库
  2. TinyFox v2.3.2 正式发布,跨平台的.NET OWIN WEB服务器
  3. QC学习一:Windows环境中Quality Center 9.0安装详解
  4. gnuplot使用1
  5. linux下IPTABLES配置详解(转)
  6. vsftp.conf
  7. postmortem report of period M1
  8. mac os快捷键
  9. unicode下各种类型转换CString、string
  10. 如何自定义echarts主题
  11. HDU1004题解分析(字符串处理)
  12. 【原创】leetCodeOj --- Dungeon Game 解题报告
  13. ECMAScript arguments 对象(摘自W3C)
  14. ubuntu解压时中文出现乱码
  15. django 报错 : django.core.exceptions.ImproperlyConfigured: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting
  16. flask项目部署
  17. DNSLog注入笔记
  18. Python_summary
  19. 配置kali linux
  20. final-review

热门文章

  1. java之进制转换
  2. 二模 (2) day2
  3. JavaScript 自定义事件
  4. 介绍几个java把网页报存为图片的框架
  5. “System.Threading.ThreadAbortException”类型的第一次机会异常在 mscorlib.dll 中发
  6. Apache虚拟主机(三)
  7. 关于struts2拦截器获取页面参数
  8. IOS中nil/Nil/NULL的区别
  9. How many instances created in the WebContainer
  10. BZOJ 1600 建造栅栏