实现思想:
把两个无序的数组首先排序,然后再按照链表结构把它们分别构造好,然后再把两个有序链表合并。
int const array1_size = 5;//数组1的长度
int const array2_size = 7;//数组2的长度
//链表结构体
typedef struct ListNode
{
int data;
ListNode * next;
}ListNode;
int const array2_size = 7;//数组2的长度
//链表结构体
typedef struct ListNode
{
int data;
ListNode * next;
}ListNode;
//合并两个有序链表返回不带头结点的头指针
ListNode * MergeList(ListNode *p,ListNode *q)
{
ListNode *h,*r;
h = new ListNode;
h->next = NULL;
r = h;
while(p !=NULL && q != NULL)
{
if(p->data <= q->data)
{
r->next = p;
r =p;
p = p->next;
}
else
{
r->next = q;
r =q;
q = q->next;
ListNode * MergeList(ListNode *p,ListNode *q)
{
ListNode *h,*r;
h = new ListNode;
h->next = NULL;
r = h;
while(p !=NULL && q != NULL)
{
if(p->data <= q->data)
{
r->next = p;
r =p;
p = p->next;
}
else
{
r->next = q;
r =q;
q = q->next;
}
}
if(p != NULL)
r->next = p;
else
r->next = q;
p = h;
h = h->next;
delete p;
return h;
}
}
if(p != NULL)
r->next = p;
else
r->next = q;
p = h;
h = h->next;
delete p;
return h;
}
//构造一个链表(没有头结点的)
ListNode * GenerateList(int array[],int length)
{
ListNode * h,*temp,*old_head ;
h = new ListNode;
h->next = NULL;
temp = h;
for(int i = 0; i< length;i++)
{
ListNode *p = new ListNode;
p->data = array[i];
temp->next = p;
temp = p;
}
temp->next = NULL;
old_head = h;
h = h->next;
delete old_head;
return h;
}
//打印链表
void Print_List(ListNode *h)
{
ListNode *p;
p = h;
for(;p!=NULL;p=p->next)
printf("%d ",p->data);
ListNode * GenerateList(int array[],int length)
{
ListNode * h,*temp,*old_head ;
h = new ListNode;
h->next = NULL;
temp = h;
for(int i = 0; i< length;i++)
{
ListNode *p = new ListNode;
p->data = array[i];
temp->next = p;
temp = p;
}
temp->next = NULL;
old_head = h;
h = h->next;
delete old_head;
return h;
}
//打印链表
void Print_List(ListNode *h)
{
ListNode *p;
p = h;
for(;p!=NULL;p=p->next)
printf("%d ",p->data);
}
//引入冒泡排序算法
//引入冒泡排序算法
void Swap(int *a,int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
void Bubble_Sort(int *array,int length)
{
int pass,j;
for(pass =1;pass<=length-1;pass++)
for(j=0;j<=length-2;j++)
if(array[j]>array[j+1])
Swap(&array[j],&array[j+1]);
}
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
void Bubble_Sort(int *array,int length)
{
int pass,j;
for(pass =1;pass<=length-1;pass++)
for(j=0;j<=length-2;j++)
if(array[j]>array[j+1])
Swap(&array[j],&array[j+1]);
}
/*********************OK,所有准备工作已经做好,开始main()函数**********/
//输入字符表示结束
int _tmain(int argc, _TCHAR* argv[])
{
char end;
int List1[array1_size]={9,5,6,10,45};
int List2[array2_size]={3,1,4,6,7,9,0};
Bubble_Sort(List1,array1_size);
Bubble_Sort(List2,array2_size);
ListNode * m_list1,*m_list2,*m_list;
m_list1 = GenerateList(List1,array1_size);
m_list2 = GenerateList(List2,array2_size);
m_list = MergeList(m_list1,m_list2);
Print_List(m_list);
scanf("%c",&end);
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
char end;
int List1[array1_size]={9,5,6,10,45};
int List2[array2_size]={3,1,4,6,7,9,0};
Bubble_Sort(List1,array1_size);
Bubble_Sort(List2,array2_size);
ListNode * m_list1,*m_list2,*m_list;
m_list1 = GenerateList(List1,array1_size);
m_list2 = GenerateList(List2,array2_size);
m_list = MergeList(m_list1,m_list2);
Print_List(m_list);
scanf("%c",&end);
return 0;
}