首页 > 其他 > 详细

将两个有序链表合并为一个新链表

时间:2015-02-04 21:33:05      阅读:248      评论:0      收藏:0      [点我收藏+]
#include <iostream>
#include <stdio.h>

typedef struct _Node
{
	int value;
	struct _Node *next;
}Node;

Node *MergeList(Node *listA, Node *listB);
void PrintList(Node *head);

int main()
{
	Node lista, nodea, listb, nodeb, nodec;
	lista.value = 2;
	nodea.value = 2;
	nodec.value = 19;
	lista.next = &nodea;
	nodea.next = &nodec;
	nodec.next = NULL;
	listb.value = 3;
	nodeb.value = 4;
	listb.next = &nodeb;
	nodeb.next = NULL;

	Node *listc = MergeList( &lista, &listb );
	PrintList(listc);
	system("pause");
	return 0;
}


/*
@brief merge two sorted lists to a new list which is sorted , too !

@param   in : listA 
@param   in : listB

@return  pHead  the new list of head
*/
Node *MergeList(Node *listA, Node *listB)
{
	Node *pHead = (Node *)malloc(sizeof(Node));
	if ( NULL == pHead )
	{
		printf("error, line %d malloc failed \n", __LINE__);
		return NULL;
	}
	pHead->next = NULL;

	Node *pNode = NULL;
	Node *pLink = pHead;

	// if the list head just is without data use 
	listA = NULL != listA ? listA->next : listA;
	listB = NULL != listB ? listB->next : listB;

	while( NULL != listA && NULL != listB )
	{
		pNode = (Node *)malloc(sizeof(Node));
		if ( NULL == pNode )
		{
			printf("error, line %d malloc failed \n", __LINE__);
			return pHead;
		}

		if ( listA->value <= listB->value )
		{
			pNode->value = listA->value;
			listA = listA->next;
		}
		else
		{
			pNode->value = listB->value;
			listB = listB->next;
		}
		pNode->next = NULL;
		pLink->next = pNode;
		pLink = pLink->next;
	}
			
	while ( NULL != listA )
	{
		pNode = (Node *)malloc(sizeof(Node));
		if ( NULL == pNode )
		{
			printf("error, line %d malloc failed \n", __LINE__);
			return pHead;
		}

		pNode->value = listA->value;
		listA = listA->next;
		pNode->next = NULL;
		pLink->next = pNode;
		pLink = pLink->next;
	}

	while ( NULL != listB )
	{
		pNode = (Node *)malloc(sizeof(Node));
		if ( NULL == pNode )
		{
			printf("error, line %d malloc failed \n", __LINE__);
			return pHead;
		}

		pNode->value = listB->value;
		listB = listB->next;
		pNode->next = NULL;
		pLink->next = pNode;
		pLink = pLink->next;
	}

	return pHead;
}

void PrintList(Node *head)
{
	if ( NULL == head )
	{
		printf("error, list is NULL\n");
		return ;
	}

	// if the list head just is without data use 
	head = head->next;

	while( NULL != head )
	{
		printf("%d\t", head->value);
		head = head->next;
	}
	printf("\n");
}

 

将两个有序链表合并为一个新链表

原文:http://www.cnblogs.com/haihuahuang/p/4273233.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!