有两个思路:
第一个思路:
先定义两个指针是分别为p,q:
p,q为工具人指针;
Node<T> *p=A.first->next;
Node<T> *q=B.first->next;
然后将A表和B表置空:
A.first->next=NULL;
B.first->next=NULL;
最后分别将A表和B表插入到C表中
while(p!=NULL)
{
Insert(p->data);
p=p->next; //这两步调用Insert函数简化操作,与下面5行代码的操作相同
/*m=p->next;
p->next=s->next;
s->next=p;
s=s->next;
p=m;*/
}
while(q!=NULL)
{
Insert(q->data);
q=q->next; //这两步调用Insert函数简化操作,与下面9行代码的操作相同
/*s=first;
while(s->next!=NULL&&s->next->data<q->data)
{
s=s->next;
}
n=q->next;
q->next=s->next;
s->next=q;
q=n;*/
}
具体代码如下:
#include<iostream>
using namespace std;
template <class T>
struct Node
{
T data;
Node<T> *next;
};
template <class T>
class LinkSortList
{
public:
LinkSortList( ); //建立只有头结点的空链表
~LinkSortList(); //析构函数
void Insert(T x); //sort insert
int Length();
void DispList( ); //遍历单链表,按序号依次输出各元素
void Merge(LinkSortList<T> &A, LinkSortList<T> &B); //合并A,B到当前类
private:
Node<T> *first; //单链表的头指针
};
template<class T>
LinkSortList<T>::LinkSortList()
{
first=new Node<T>;
first->next=NULL;
}
template<class T>
LinkSortList<T>::~LinkSortList()
{
Node<T> *p=first;
while(first!=NULL) //释放每一个节点的储存空间
{
first=first->next; //first指向被释放节点的下一个节点
delete p;
p=first; //工作人指针p后移
}
}
template<class T>
void LinkSortList<T>::Insert(T x)
{
Node<T> *p=first,*s=new Node<T>;
s->data=x;
while(p->next!=NULL&&x>p->next->data)
{
p=p->next;
}
s->next=p->next;
p->next=s;
}
template<class T>
int LinkSortList<T>::Length()
{
int num=0;
Node<T> *p=first->next;
while(p!=NULL)
{
num++;
p=p->next;
}
return num;
}
template<class T>
void LinkSortList<T>::DispList()
{
cout<<"The length:"<<Length()<<endl;
cout<<"The elements:";
Node<T> *p=first->next;
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
template<class T>
void LinkSortList<T>::Merge(LinkSortList<T> &A, LinkSortList<T> &B)
{
Node<T> *p=A.first->next;
Node<T> *q=B.first->next;
/*Node<T> *s=first;*/
Node<T> *m;
Node<T> *n;
A.first->next=NULL;
B.first->next=NULL;
while(p!=NULL)
{
m=p->next;
p->next=s->next;
s->next=p;
s=s->next;
p=m;
}
while(q!=NULL)
{
s=first;
while(s->next!=NULL&&s->next->data<q->data)
{
s=s->next;
}
n=q->next;
q->next=s->next;
s->next=q;
q=n;
}
}
int main( ){
LinkSortList <int> A,B,C;
int x;
while(1){
cin>>x;
if(!x) break;
A.Insert(x);
}
while(1){
cin>>x;
if(!x) break;
B.Insert(x);
}
A.DispList();
B.DispList();
C.Merge(A,B);
C.DispList();
A.DispList();
B.DispList();
return 0;
}
第二个思路:
第二步的操作一样,只是第一步需要定义三个指针:
Node<T> *p=A.first->next;
Node<T> *q=B.first->next;
Node<T> *s=first;
其中s也是一个工具人指针;
最后一步再改动一下:
第一种思路呢是分别插入两个表,那换一种思路想,可以直接比较A,B两表中数的大小,然后插入C表,计算过程中一定会出现有一个表的数数字没有加完,所以就需要区别,然后继续插入,具体代码如下:
template <class T>
void LinkSortList<T>::Merge(LinkSortList<T> &A,LinkSortList<T> &B)
{
Node<T> *p=A.first->next;
Node<T> *q=B.first->next;
Node<T> *s=first;
A.first->next=NULL;
B.first->next=NULL;
while(p!=NULL&&q!=NULL)
{
if(p->data<=q->data)
{
s->next=p;
p=p->next;
s=s->next;
}
else
{
s->next=q;
q=q->next;
s=s->next;
}
} //上面的代码一定会出现有一个表的数字没有加完,所以就需要下面的代码来区别,然后继续插入
if(q!=NULL)
{
Insert(q->data);
q=q->next;
}
if(p!=NULL)
{
Insert(p->data);
p=p->next;
}
}