问题一:若两表都是顺序表,则可以运用归并排序的思想直接合并两张表,从而达到时间复杂度最小。
核心代码如下:
Conlink Conbine(Conlink LA,Conlink LB,Conlink LC)
{
cout<<"conbine:";
int i=0,j=0,k=0;
while(i<=LA.last&&j<=LB.last) //table A and B is not empty
{
if(LA.date[i]<LB.date[j]) //while is the little,which insert into the C
{
LC.date[k]=LA.date[i];
i++;
k++;
}
else
{
LC.date[k]=LB.date[j];
j++;
k++;
}
}
while(i<=LA.last) //to save the rest of the List A
{
LC.date[k]=LA.date[i];
i++;
k++;
}
while(j<LB.last) //to save the rest of the List B
{
LC.date[k]=LB.date[j];
k++;
j++;
}
LC.last=k-1; //to check the last
return LC;
}
上述核心算法即运用已经有序这一个条件,来合并两表,比一个一个数遍历起来时间复杂度小得多。
完整代码如下:
//creat two Sequential table and conbine the two to the third
#include<iostream>
using namespace std;
typedef int elemtype; //the true type you can define it yourself
#define MAXSIZE 100 //you can define it yourself
typedef struct ConLinklist
{
elemtype date[MAXSIZE];
int last;
}Conlink;
Conlink Creat(Conlink L,int n); //creat new Sequential table
Conlink Conbine(Conlink LA,Conlink LB,Conlink LC); //conbine the two table
void Output(Conlink L); //to display the two
Conlink Creat(Conlink L)
{
int i=0,n;
L.last=-1;
cout<<"please input the number of the figure in the list:";
cin>>n;
cout<<endl;
for(i=0;i<n;i++)
{
if(L.last<MAXSIZE)
{
L.last++;
cout<<"please input the "<<i+1<<" figure:";
cin>>L.date[L.last];
}
}
return L;
}
Conlink Conbine(Conlink LA,Conlink LB,Conlink LC)
{
cout<<"conbine:";
int i=0,j=0,k=0;
while(i<=LA.last&&j<=LB.last) //table A and B is not empty
{
if(LA.date[i]<LB.date[j]) //while is the little,which insert into the C
{
LC.date[k]=LA.date[i];
i++;
k++;
}
else
{
LC.date[k]=LB.date[j];
j++;
k++;
}
}
while(i<=LA.last) //to save the rest of the List A
{
LC.date[k]=LA.date[i];
i++;
k++;
}
while(j<LB.last) //to save the rest of the List B
{
LC.date[k]=LB.date[j];
k++;
j++;
}
LC.last=k-1; //to check the last
return LC;
}
void Output(Conlink L)
{
int i=0;
cout<<"the result is:"<<endl;
for(i=0;i<=L.last;i++)
cout<<L.date[i]<<" ";
cout<<endl;
}
int main()
{
Conlink LA,LB,LC;
LA=Creat(LA);
Output(LA);
LB=Creat(LB);
Output(LB);
LC=Conbine(LA,LB,LC);
Output(LC);
return 0;
}
问题二:若两表为链表,该如何合并:
算法思想:采用归并排序逆向用的最基本的思想来合并,从而保证有序而且时间复杂度为O(m+n)。
具体代码如下:
//to conbine two Linklist to be one
#include<iostream>
using namespace std;
typedef int elemtype;
typedef struct ConLinklist
{
elemtype date;
struct ConLinklist *next;
}*Linklist,Conlink;
Linklist Creat(Linklist L); //creat a new Linklist
Linklist Conbine(Linklist LA,Linklist LB); // conbine two Linklist
void Output(Linklist LD); //to show the linklist
//the function to creat two Linklist
Linklist Creat(Linklist L,int n)
{
Linklist p,r;
int i;
L=new(Conlink);
L->next=NULL;
p=L;
for(i=0;i<n;i++)
{
r = new(Conlink);
p->next=r;
p=r;
cout<<"please input the "<<i+1<<" number:";
cin>>p->date;
}
p->next=NULL;
return L;
}
//the function to link the two list
Linklist Conbine(Linklist LA,Linklist LB,Linklist LC)
{
Linklist p,q,r;
p=LA->next;
q=LB->next;
r=LC;
cout<<"Conbine:";
while(p!=NULL&&q!=NULL)
if(p->date<q->date)
{
r->next=p;
r=p;
p=p->next;
}
else
{
r->next=q;
r=q;
q=q->next;
}
while(p)
{
r->next=p;
r=p;
p=p->next;
}
while(q)
{
r->next=q;
r=q;
q=q->next;
}
r->next=NULL;
return LC;
}
//the function to display the list
void Output(Linklist LD)
{
Linklist p;
p=LD->next;
while(p)
{
cout<<p->date<<" ";
p=p->next;
}
cout<<endl;
}
//main
int main()
{
Linklist LA=NULL,LB=NULL,LC;
LC=new(Conlink);
LA=Creat(LA,6);
Output(LA);
LB=Creat(LB,3);
Output(LB);
LC=Conbine(LA,LB,LC);
Output(LC);
return 0;
}
代码计算结果为成功合并,其中核心为Conbine函数,链表的构建用的是尾插法,保证和自己输入的升序一样保持升序。