合并两个链表,可以分为有序和无序的链表。
无序链表非常简单,将一个链表接在另一个链表的后面就可以了。
但是如果是有序的两个链表就设计到判断和一些细节,这里主要实现两个递增有序的链表。
合并方案一:采用递归方式
无序链表非常简单,将一个链表接在另一个链表的后面就可以了。
但是如果是有序的两个链表就设计到判断和一些细节,这里主要实现两个递增有序的链表。
合并方案一:采用递归方式
#include <iostream>
#include <algorithm>
#include <list>
using namespace std;
struct List
{
int number;
List *pNext;
};
List *pHead1=NULL;
List *pHead2=NULL;
List *pEnd=NULL;
List *pNode=NULL;
void Insert_List(List **pHead,int data)
{
pNode=new List;
pNode->number=data;
pNode->pNext=NULL;
if(!(*pHead))//空链表
{
*pHead=pNode;
pEnd=pNode;
return;
}
else //非空链表
{
List *temp=*pHead;
//插入到头结点
if(temp->number > data)
{
pNode->pNext=temp;
*pHead=pNode;
return;
}
//插入到非头结点;
while(temp->pNext && temp->number < data)
{
temp=temp->pNext;
}
//插入新节点
pNode->pNext=temp->pNext;
temp->pNext=pNode;
}
}
void show(List *pHead)
{
List *temp=pHead;
cout<<"List:";
while(temp)
{
cout<<temp->number<<" ";
temp=temp->pNext;
}
cout<<endl;
}
List* MergeList(List *list1,List * list2)
{
if(!list1 || !list2)//若有一个链表为空;
return list2 ? list2:list1;
List *MergeHead=NULL;
if(list1->number<list2->number)
{
MergeHead=list1;
MergeHead->pNext=MergeList(list1->pNext,list2);
}
else
{
MergeHead=list2;
MergeHead->pNext=MergeList(list1,list2->pNext);
}
return MergeHead;
}
int main()
{
int array1[5]={4,6,2,8,0};//第一个个链表数据;
int array2[5]={3,1,7,5,9};//第二个链表的数据;
for(int i=0;i<5;i++)
{
Insert_List(&pHead1,array1[i]);
}
show(pHead1);
for(int i=0;i<5;i++)
{
Insert_List(&pHead2,array2[i]);
}
show(pHead2);
show(MergeList(pHead1,pHead2));//递归合并输出
system("pause");
return 0;
}
运行结果:
List* MgrgeList(List* list1,List*list2)
{
List *NewHead=NULL;
List *last=NULL;
if(!list1 || !list2)
return list1?list1:list2;
if(list1->number<list2->number)
{
NewHead=last=list1;
list1=list1->pNext;
}
else
{
NewHead=last=list2;
list2=list2->pNext;
}
while(list1 && list2)
{
if(list1->number<list2->number)
{
last->pNext=list1;
last=list1;
list1=list1->pNext;
}
else
{
last->pNext=list2;
last=list2;
list2=list2->pNext;
}
}
last->pNext=(list1?list1:list2);
return NewHead;
}
运行结果:
利用容器list可以很快的实现:但是只有merge函数能完美的解决我们的问题!详细原因看代码和结果!
#include <iostream>
using namespace std;
int main()
{
int array1[5]={4,6,2,8,0};//第一个个链表数据;
int array2[5]={3,1,7,5,9};//第二个链表的数据;
list<int> pHead1;
list<int> pHead2;
list<int>::iterator it1;
list<int>::iterator it2;
for(int i=0;i<5;i++) //第二个链表;
{
pHead1.push_back(array1[i]);
}
pHead1.sort(); //排序;
cout<<"List1:";
for(it1=pHead1.begin();it1!=pHead1.end();it1++)
{
cout<<*it1<<" ";
}
cout<<endl;
for(int i=0;i<5;i++) //第二个链表;
{
pHead2.push_back(array2[i]);
}
pHead2.sort();
cout<<"List2:";
for(it2=pHead1.begin();it2!=pHead1.end();it2++)
{
cout<<*it2<<" ";
}
cout<<endl;
// pHead1.insert(pHead1.begin(),pHead2.begin(),pHead2.end()); //代码段一,运行结果如图结果一。
//将list2插入到list1的前面,没有排序;合并后list2依旧存在;
pHead1.merge(pHead2);//合并连个链表;合并后list2没有了!//代码段二,运行结果如结果图二,也只有merge才能解决我们的问题。
//it1=pHead1.end(); //代码段三,运行结果如结果图三。
//pHead1.splice(it1,pHead2);//将list2插入到list1链表的后面,不重新排序;但是合并后的list1就没有了;
cout<<"Merge:";
for(it1=pHead1.begin();it1!=pHead1.end();it1++)
{
cout<<*it1<<" ";
}
cout<<endl;
if(pHead2.empty())
{
cout<<"List2 is NULL"<<endl;
}
else
{
cout<<"List2 is still here!"<<endl;
}
system("pause");
return 0;
}
insert运行结果:
结果一
Merge运行结果:结果二
splice运行结果:
结果三