Data Structure - 2.2 Exercises nondecreasing sorted linked list ordered merge non increments

1. Title Description

  • The two non-decreasing order of non-increasing list into one ordered list. The results list is still required to use storage space of the original two lists, not otherwise occupied by other storage space. Table allows duplicate data.

2. Topic analysis

  • The combined use of the new table L3 head pointer points, p1 and p2 are the L1 and L2 work list pointer is initialized to a first node of a respective linked list, beginning from the first node is performed by comparing two lists L1 when the end is reached and L2 are both node, wherein the removal of successively smaller relinked L3 table after the header node , if the two elements are equal in the table, only the removal of the elements of the table L1, L2 retained table of elements. When a node table reaches the end of the table, is empty, the remaining elements of non-empty list sequentially picking, link L3 header node table after.
​// 链表合并
void Combine(LinkList &L1, LinkList &L2, LinkList &L3)
{
	LNode *p1, *p2, *p3, *q;
	p1 = L1->next;
	p2 = L2->next;  // p1和p2分别是链表L1和L2的工作指针,初始化为相应链表的第一个结点
	p3 = L3 = L1;   // 用L1的头结点作为L3的头结点
	L3->next = NULL;
	
	while (p1 || p2)   // 只要存在一个非空表,用q指向待摘取的元素
	{					
		if (!p1)   // L1表为空,用q指向p2,p2指针后移
		{ 
			q = p2;  
			p2 = p2->next; 
		}
		else if (!p2)    // L2表为空,用q指向p1,p1指针后移
		{	
			q = p1;  
			p1 = p1->next; 
		}
		
		else if (p1->data <= p2->data) // 取较小者(包括相等)L1中的元素,用q指向p1,p1指针后移
		{ 
			q = p1;  
			p1 = p1->next; 
		}
		else    // 取较小者L2中的元素,用q指向p12,p2指针后移
		{ 
			q = p2; 
			p2 = p2->next; 
		}
		
		q->next = L3->next;  
		L3->next = q;       // 将q指向的结点插在Lc 表的表头结点之后
	}
	
	delete L2;             //释放Lb的头结点
}

3. code implementation

  • main.cpp
#include <iostream>

using namespace std;

typedef struct LNode
{
	int data;
	struct LNode *next;
}LNode, *LinkList;

int InitList(LinkList &L)
{
	L = new LNode;
	L->next = NULL;
	return 1;
}

void TraveList(LinkList L)
{
	LNode *p;
	p = L->next;

	while (p)
	{
		printf("%d ", p->data);
		p = p->next;
	}
	printf("\n");
}

// 尾插法建立链表
void CreateList(LinkList &L, int n)
{
	L = new LNode;
	L->next = NULL;
	LNode *r;
	r = L;
	for (int i = 0; i < n; i++)
	{
		printf("请输入链表第%d个元素的值:", i + 1);
		LNode *s;
		s = new LNode;
		scanf("%d", &s->data);
		s->next = NULL;
		r->next = s;
		r = s;
	}
}

// 链表合并
void Combine(LinkList &L1, LinkList &L2, LinkList &L3)
{
	LNode *p1, *p2, *p3, *q;
	p1 = L1->next;
	p2 = L2->next;  // p1和p2分别是链表L1和L2的工作指针,初始化为相应链表的第一个结点
	p3 = L3 = L1;   // 用L1的头结点作为L3的头结点
	L3->next = NULL;
	
	while (p1 || p2)   // 只要存在一个非空表,用q指向待摘取的元素
	{					
		if (!p1)   // L1表为空,用q指向p2,p2指针后移
		{ 
			q = p2;  
			p2 = p2->next; 
		}
		else if (!p2)    // L2表为空,用q指向p1,p1指针后移
		{	
			q = p1;  
			p1 = p1->next; 
		}
		
		else if (p1->data <= p2->data) // 取较小者(包括相等)L1中的元素,用q指向p1,p1指针后移
		{ 
			q = p1;  
			p1 = p1->next; 
		}
		else    // 取较小者L2中的元素,用q指向p12,p2指针后移
		{ 
			q = p2; 
			p2 = p2->next; 
		}
		
		q->next = L3->next;  
		L3->next = q;       // 将q指向的结点插在Lc 表的表头结点之后
	}
	
	delete L2;             //释放Lb的头结点
}
	
int main()
{
	LinkList L1, L2, L3;

	if (InitList(L1))
	{
		printf("L1初始化成功!\n");
	}
	else
	{
		printf("L1初始化失败!\n");
	}

	if (InitList(L2))
	{
		printf("L2初始化成功!\n");
	}
	else
	{
		printf("L2初始化失败!\n");
	}

	printf("请输入L1的长度:");
	int n1;
	scanf("%d", &n1);
	CreateList(L1, n1);
	TraveList(L1);

	printf("请输入L2的长度:");
	int n2;
	scanf("%d", &n2);
	CreateList(L2, n2);
	TraveList(L2);

	Combine(L1, L2, L3);
	printf("合并后的链表:\n");
	TraveList(L3);

	system("pause");

	return 0;
}
  • Test Results

Guess you like

Origin blog.csdn.net/qq_22847457/article/details/94133604