Data Structure - 2.3 Exercises request list is incremented two intersection

1. Problem Description

  • Known two lists L1 and L2 denote two sets whose elements are ascending. Please design algorithm derived L1 and L2 intersection, and stored in the L1 list.

2. Topic analysis

  • Also appear in only two elements of a set to appear in the results table, 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 compares the two lists when both L1 and L2 when the table reaches the end node, If one of the elements in the table is small, remove smaller elements in this table, the table pointer of the work; table if the two elements are equal, removal of the table elements L1, L2 delete elements in the table shift. When La and Lb have a list table reaches the end node is empty, sequentially deletes all non-empty elements of another table.
// 链表交集
void Intersection(LinkList &L1, LinkList &L2, LinkList &L3)
{
	LNode *p1, *p2, *p3, *q, *u;
	p1 = L1->next;
	p2 = L2->next;  // p1和p2分别是链表L1和L2的工作指针,初始化为相应链表的第一个结点
	p3 = L3 = L1;   // 用L1的头结点作为L3的头结点
	
	while (p1&&p2)
	{
		if (p1->data == p2->data)    //交集并入结果表中。
		{ 
			p3->next = p1; 
			p3 = p1; 
			p1 = p1->next;
			u = p2; 
			p2 = p2->next; 
			delete u; 
		}
		else if (p1->data < p2->data) 
		{ 
			u = p1; 
			p1 = p1->next; 
			delete u; 
		}
		else 
		{ 
			u = p2; 
			p2 = p2->next; 
			delete u; 
		}
	}
	while (p1) // 释放结点空间
	{ 
		u = p1;
		p1 = p1->next; 
		delete u; 
	}
	
	while (p2)   // 释放结点空间
	{
		u = p2; 
		p2 = p2->next; 
		delete u;
	}
	
	p3->next = NULL;    // 置链表尾标记。
	
	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 Intersection(LinkList &L1, LinkList &L2, LinkList &L3)
{
	LNode *p1, *p2, *p3, *q, *u;
	p1 = L1->next;
	p2 = L2->next;  // p1和p2分别是链表L1和L2的工作指针,初始化为相应链表的第一个结点
	p3 = L3 = L1;   // 用L1的头结点作为L3的头结点
	
	while (p1&&p2)
	{
		if (p1->data == p2->data)    //交集并入结果表中。
		{ 
			p3->next = p1; 
			p3 = p1; 
			p1 = p1->next;
			u = p2; 
			p2 = p2->next; 
			delete u; 
		}
		else if (p1->data < p2->data) 
		{ 
			u = p1; 
			p1 = p1->next; 
			delete u; 
		}
		else 
		{ 
			u = p2; 
			p2 = p2->next; 
			delete u; 
		}
	}
	while (p1) // 释放结点空间
	{ 
		u = p1;
		p1 = p1->next; 
		delete u; 
	}
	
	while (p2)   // 释放结点空间
	{
		u = p2; 
		p2 = p2->next; 
		delete u;
	}
	
	p3->next = NULL;    // 置链表尾标记。
	
	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");
	}

	if (InitList(L3))
	{
		printf("L3初始化成功!\n");
	}
	else
	{
		printf("L3初始化失败!\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);

	Intersection(L1, L2, L3);
	printf("链表交集:\n");
	TraveList(L3);

	system("pause");

	return 0;
}
  • operation result

Guess you like

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