题目:
将两个已经排好序的链表组合成一个排序的链表。
/*
归并排序的思想
时间:2015年4月8日 19:59:02
*/
#include <iostream>
#include <cstdlib>
using namespace std;
typedef struct Node //模拟节点
{
int data;
struct Node * next;
}NODE,* PNODE;
class List
{
private:
PNODE pHead;
int len; //链表长度
public:
List(int _len=0); //初始化链表
List(List & blist); //拷贝构造函数
~List();
void create()const; //随机创建一个链表
void traverse1()const; //遍历
void bubble_sort(); //冒泡排序
PNODE Merge(List & blist)const; //归并排序
int show(){return len;} //返回长度
};
void traverse2(PNODE pHead);
int main()
{
int len1,len2;
cout<<"请输入一个大于0的数作为第一个链表的长度: ";
cin>>len1;
cout<<"请输入一个大于0的数作为第二个链表的长度: ";
cin>>len2;
List A(len1); //长度为len1的链表
A.create(); //创建A对象链表
cout<<"链表A有"<<A.show()<<"个元素"<<",未排序时:"<<endl;
A.traverse1();
cout<<"链表A排序后:"<<endl;
A.bubble_sort(); //排序
A.traverse1();
cout<<endl<<endl;
List B(len2); //长度为len1的链表
B.create(); //创建B对象链表
cout<<"链表B有"<<B.show()<<"个元素"<<",未排序时:"<<endl;
B.traverse1();
cout<<"链表B排序后:"<<endl;
B.bubble_sort(); //排序
B.traverse1();
cout<<endl<<endl;
cout<<"归并后的链表为:"<<endl;
PNODE p =A.Merge(B);
traverse2(p);
cout<<endl<<endl;
return 0;
}
List::List(int _len ) //默认链表长度为0
{
pHead = new NODE;
pHead->next = NULL;
len = _len;
}
List::List(List & blist) //拷贝构造函数
{
pHead = new NODE;
PNODE pTail = pHead;
pTail->next = NULL;
len = blist.len;
for(int i=0; i<len; i++)
{
PNODE pNew = new NODE;
pNew->data = blist.pHead->data;
pTail->next = pNew;
pNew->next = NULL;
pTail = pNew;
blist.pHead = blist.pHead->next;
}
}
List::~List() //删除链表
{
NODE * temp = NULL;
while(pHead != NULL)
{
temp = pHead->next;
delete pHead;
pHead = temp;
}
}
void List::create()const //创建长度为len的链表
{
if(len <= 0)
return;
int i;
PNODE pTail = new NODE;
pTail->next = NULL;
pTail = pHead;
for(i=0; i<len; i++)
{
PNODE pNew = new NODE;
pNew->data = rand()%1000; //1000以内的随机非负数
pTail->next = pNew;
pNew->next = NULL;
pTail = pNew;
}
return;
}
void List::traverse1()const //遍历
{
PNODE temp = pHead->next;
while(temp != NULL)
{
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
return;
}
void List::bubble_sort()
{
if(len <= 1) //长度小于等于1,则不用排序
return;
int temp;
PNODE pTemp = pHead->next;
PNODE pNew =pTemp;
for(int i=0; i<len-1; i++)
{
for(int j=0; j<len-i-1; j++ )
{
if( pNew->next && pNew->data > pNew->next->data )
{
temp = pNew->data;
pNew->data = pNew->next->data;
pNew->next->data = temp;
}
pNew = pNew->next;
}
pNew = pTemp;
}
return ;
}
PNODE List::Merge(List & blist) const //归并
{
int m=0, n=0;//m计数本对象,n计数形参对象
PNODE m_temp = pHead->next;
PNODE n_temp = blist.pHead->next;
PNODE p = new NODE;
PNODE pTail = p;
pTail->next = NULL;
while(m<len && n<blist.len) //当有一个链表比较完了,另外一个链表只要接上去就OK
{
if(m_temp->data < n_temp->data)
{
PNODE pNew = new NODE;
pNew->data = m_temp->data;
pTail->next = pNew;
pNew->next = NULL;
pTail = pNew;
m_temp = m_temp->next;
m++;
}
else if(m_temp->data > n_temp->data)
{
PNODE pNew = new NODE;
pNew->data = n_temp->data;
pTail->next = pNew;
pNew->next = NULL;
pTail = pNew;
n_temp = n_temp->next;
n++;
}
}
//看哪个链表还有没比较完的元素
if( m<len )
{
pTail->next = m_temp;
}
else if(n<blist.len)
{
pTail->next = n_temp;
}
return p;
}
void traverse2(PNODE pHead) //这个不是类里的遍历函数
{
PNODE temp = pHead->next;
while(temp != NULL)
{
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
return;
}