C++ 合并链表

合并2个递增链表,使得合并后仍保持递增顺序:

MergeList.cpp:

/*合并2个排序的链表,2个递增的排序链表,合并这2个使得新链表中的结点仍是按照递增顺序排列的*/
#include <iostream>
#include <stdio.h>
#include "List.h"
using namespace std;
ListNode* MergeList(ListNode* pHead1,ListNode* pHead2)
{
	if(pHead1 == NULL)
		return pHead2;
	if(pHead2 == NULL)
		return pHead1;
	ListNode* pMerge = NULL;
	if(pHead1 ->m_value <= pHead2 -> m_value)
	{
		pMerge = pHead1;
		pMerge -> m_pNext = MergeList(pHead1->m_pNext ,pHead2);
	}
	else
	{
		pMerge = pHead2;
		pHead2 -> m_pNext = MergeList(pHead1,pHead2->m_pNext);
	}
	return pMerge;
}
// ====================测试代码====================  
ListNode* Test(char* testName, ListNode* pHead1, ListNode* pHead2)  
{  
    if(testName != NULL)  
        cout<<"%s begins:"<< testName<<endl;  
  
    cout<<"The first list is:";  
    PrintList(pHead1);  
  
    cout<<"\n"<<"The second list is:";  
    PrintList(pHead2);  
  
    cout<<"\n"<<"The merged list is:";  
    ListNode* pMergedHead = MergeList(pHead1, pHead2);  
    PrintList(pMergedHead);  
	cout << endl;
    return pMergedHead;  
}  
  
// list1: 1->3->5  
// list2: 2->4->6  
void Test1()  
{  
    ListNode* pNode1 = CreateListNode(1);  
    ListNode* pNode3 = CreateListNode(3);  
    ListNode* pNode5 = CreateListNode(5);  
  
    ConnectListNodes(pNode1, pNode3);  
    ConnectListNodes(pNode3, pNode5);  
  
    ListNode* pNode2 = CreateListNode(2);  
    ListNode* pNode4 = CreateListNode(4);  
    ListNode* pNode6 = CreateListNode(6);  
  
    ConnectListNodes(pNode2, pNode4);  
    ConnectListNodes(pNode4, pNode6);  
  
    ListNode* pMergedHead = Test("Test1", pNode1, pNode2);  
  
    DestroyList(pMergedHead);  
}  
  
// 两个链表中有重复的数字  
// list1: 1->3->5  
// list2: 1->3->5  
void Test2()  
{  
    ListNode* pNode1 = CreateListNode(1);  
    ListNode* pNode3 = CreateListNode(3);  
    ListNode* pNode5 = CreateListNode(5);  
  
    ConnectListNodes(pNode1, pNode3);  
    ConnectListNodes(pNode3, pNode5);  
  
    ListNode* pNode2 = CreateListNode(1);  
    ListNode* pNode4 = CreateListNode(3);  
    ListNode* pNode6 = CreateListNode(5);  
  
    ConnectListNodes(pNode2, pNode4);  
    ConnectListNodes(pNode4, pNode6);  
  
    ListNode* pMergedHead = Test("Test2", pNode1, pNode2);  
  
    DestroyList(pMergedHead);  
}  
  
// 两个链表都只有一个数字  
// list1: 1  
// list2: 2  
void Test3()  
{  
    ListNode* pNode1 = CreateListNode(1);  
    ListNode* pNode2 = CreateListNode(2);  
  
    ListNode* pMergedHead = Test("Test3", pNode1, pNode2);  
  
    DestroyList(pMergedHead);  
}  
  
// 一个链表为空链表  
// list1: 1->3->5  
// list2: 空链表  
void Test4()  
{  
    ListNode* pNode1 = CreateListNode(1);  
    ListNode* pNode3 = CreateListNode(3);  
    ListNode* pNode5 = CreateListNode(5);  
  
    ConnectListNodes(pNode1, pNode3);  
    ConnectListNodes(pNode3, pNode5);  
  
    ListNode* pMergedHead = Test("Test4", pNode1, NULL);  
  
    DestroyList(pMergedHead);  
}  
  
// 两个链表都为空链表  
// list1: 空链表  
// list2: 空链表  
void Test5()  
{  
    ListNode* pMergedHead = Test("Test5", NULL, NULL);  
}  
  
int main()  
{  
    Test1();  
    Test2();  
    Test3();  
    Test4();  
    Test5();  
	system("PAUSE");
    return 0;  
}  
链表基本结构及操作:

List.h:

#include <iostream>
#include <stdio.h>
using namespace std;
struct ListNode
{
	int m_value;
	ListNode *m_pNext;
};
ListNode* CreateListNode(int value);  
void ConnectListNodes(ListNode* pCurrent, ListNode* pNext);  
void PrintListNode(ListNode* pNode);  
void PrintList(ListNode* pHead);  
void DestroyList(ListNode* pHead);  
void AddToTail(ListNode** pHead, int value);  
void RemoveNode(ListNode** pHead, int value);  

List.cpp:

#include <iostream>
#include <stdio.h>
#include "List.h"
using namespace std;
ListNode* CreateListNode(int value)
{
	ListNode *n_Node = new ListNode;
	n_Node -> m_value = value;
	n_Node ->m_pNext = NULL;
	return n_Node;
}
void ConnectListNodes(ListNode* pCurrent,ListNode* pNext)
{
	if(!pCurrent)
	{
		cout<< "Error: Can't connect the nodes"<<endl;
		exit(1);
	}
	pCurrent -> m_pNext = pNext;
}
void PrintListNode(ListNode* pNode)
{
	if(!pNode)
	{
		cout<< "Error: can't print the null pointer"<<endl;
		exit(1);
	}
	cout << pNode -> m_value<<endl;
}
void PrintList(ListNode* pHead)
{
	//cout<< "Here is the list: ";
	ListNode* pNode = pHead;
	while(pNode)
	{
		//PrintListNode(pNode);
		cout << pNode ->m_value;
		pNode = pNode -> m_pNext;
		if(pNode)
			cout << "->";
	}
//	cout<< "\nThe print is done"<<endl;
}
void DestroyList(ListNode* pHead)
{
	cout << "Destroyment starts:";
	ListNode* pNode =pHead;
	while(pNode)
	{
		pHead = pNode ->m_pNext;
		delete pNode;
		pNode = pHead;
	}
	cout << "Destroyment finish"<<endl;
}
void AddToTail(ListNode** pHead,int value)
{
	ListNode* pNew = new ListNode;
	pNew -> m_value = value;
	pNew -> m_pNext = NULL;
	ListNode* pNode = *pHead;
	if(*pHead == NULL)
		*pHead = pNew;
	else
	{
		while(pNode->m_pNext)
			pNode = pNode->m_pNext;
		pNode -> m_pNext = pNew;
	}
}
void RemoveNode(ListNode** pHead,int value)
{
	if(*pHead == NULL|| pHead ==NULL)
	{
		cout << "Error:can't remove the node from a null list"<<endl;
		exit(1);
	}
	else
	{
		ListNode* pNode = *pHead;
		ListNode* pDelete = NULL;
		if(pNode -> m_value == value)
		{
			pDelete = pNode;
			*pHead = pNode ->m_pNext;
		}else
		{
			while(pNode->m_pNext)
			{
				if(pNode->m_pNext ->m_value == value)
				{
					pDelete = pNode->m_pNext;
					pNode ->m_pNext = pDelete ->m_pNext;
					delete pDelete;
					return;
				}
				else
				{
					pNode = pNode ->m_pNext;
				}
			}
			if(!pNode->m_pNext)
			{
				cout << "Error: the node doesn't exist in the list"<<endl;
				exit(1);
			}
		}
		if(pDelete)
			delete pDelete;
		pDelete = NULL;
	}
}

运行结果:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值