您的位置:首页 > Web前端

【剑指offer】链表相关-合并两个有序链表&递归写法17

2014-03-04 23:21 537 查看
#include<iostream.h>
#include <stdio.h>
#include <stack>
//合并两个有序链表
struct ListNode
{
int m_nValue;
ListNode * m_pNext;
};
ListNode * CreateLink(int a[],int k)
{
ListNode * Head=NULL,*q=NULL;
for(int i=0;i<k;i++)
{
ListNode * pNew=new ListNode();
pNew->m_nValue=a[i];
pNew->m_pNext=NULL;

if(Head==NULL)
{
Head=pNew;
q=pNew;
}
else
{
q->m_pNext=pNew;
q=q->m_pNext;
}
}
return Head;
}
//从头到尾打印列表
void printLink(ListNode * pHead)
{
cout<<"链表内容为:";
ListNode *p=pHead;
while(p)
{
cout<<p->m_nValue<<" ";
p=p->m_pNext;
}
cout<<endl;
}
//自己写的程序
ListNode * Merge1(ListNode * pHead1,ListNode * pHead2)
{
ListNode * newList=NULL,*p=NULL;
if(pHead1==NULL && pHead2==NULL)
return newList;
if(pHead1==NULL)
return pHead2;
if(pHead2==NULL)
return pHead1;
while(pHead1!=NULL && pHead2!=NULL)
{
if(pHead1->m_nValue >= pHead2->m_nValue )
{
//处理新链表的第一个结点
if(newList==NULL)
{
newList=pHead2;
pHead2=pHead2->m_pNext;
p=newList;
p->m_pNext=NULL;
}
else
{
p->m_pNext=pHead2;
pHead2=pHead2->m_pNext;
p=p->m_pNext;
p->m_pNext=NULL;
}
}
else
{
//处理新链表的第一个结点
if(newList==NULL)
{
newList=pHead1;
pHead1=pHead1->m_pNext;
p=newList;
p->m_pNext=NULL;
}
else
{
p->m_pNext=pHead1;
pHead1=pHead1->m_pNext;
p=p->m_pNext;
p->m_pNext=NULL;
}
}
}
if(pHead1==NULL)
{
p->m_pNext=pHead2;
}
if(pHead2==NULL)
{
p->m_pNext=pHead1;
}
return newList;
}
//书上的
ListNode * Merge(ListNode * pHead1,ListNode * pHead2)
{
if(pHead1 == NULL)
return pHead2;
if(pHead2 == NULL)
return pHead1;
ListNode * pMergedHead=NULL;
if(pHead1->m_nValue < pHead2->m_nValue )
{
pMergedHead=pHead1;
pMergedHead->m_pNext=Merge(pHead1->m_pNext,pHead2);
}
else
{
pMergedHead=pHead2;
pMergedHead->m_pNext=Merge(pHead2->m_pNext,pHead1);
}
return pMergedHead;
}

//=======测试用例=========
//1.合并两个空链表
//2.其中一个为空链表
//3.其中一个为一个节点

void Test1()
{
cout<<"测试用例test1"<<endl;
ListNode * ptr1=NULL;
ListNode * ptr2=NULL;
printLink(ptr1);
printLink(ptr2);

ListNode * newList=Merge(ptr1,ptr2);

printLink(newList);
}
void Test2()
{
cout<<"测试用例test2"<<endl;
int a[]={1,2,3,4};

ListNode * ptr1=CreateLink(a,4);
ListNode * ptr2=NULL;
printLink(ptr1);
printLink(ptr2);

ListNode * newList=Merge(ptr1,ptr2);

printLink(newList);
}
void Test3()
{
cout<<"测试用例test3"<<endl;
int a[]={1,2,3,4};

ListNode * ptr1=NULL;
ListNode * ptr2=CreateLink(a,4);
printLink(ptr1);
printLink(ptr2);

ListNode * newList=Merge(ptr1,ptr2);

printLink(newList);
}
void Test4()
{
cout<<"测试用例test4"<<endl;
int a[]={1,2,3,4};
int b[]={2,4,6,7};

ListNode * ptr1=CreateLink(a,4);
ListNode * ptr2=CreateLink(b,4);
printLink(ptr1);
printLink(ptr2);

ListNode * newList=Merge(ptr1,ptr2);

printLink(newList);
}
void Test5()
{
cout<<"测试用例test5"<<endl;
int a[]={1,2,3,4};
int b[]={2};

ListNode * ptr1=CreateLink(a,4);
ListNode * ptr2=CreateLink(b,1);
printLink(ptr1);
printLink(ptr2);

ListNode * newList=Merge(ptr1,ptr2);

printLink(newList);
}
void Test6()
{
cout<<"测试用例test6"<<endl;
int a[]={1,2,3,4};
int b[]={2,2,2};

ListNode * ptr1=CreateLink(a,4);
ListNode * ptr2=CreateLink(b,3);
printLink(ptr1);
printLink(ptr2);

ListNode * newList=Merge(ptr1,ptr2);

printLink(newList);
}
void main()
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: