数据结构之SWUSTOJ1037: 集合的并运算的实现

题目:

 

代码:

#include<stdio.h>
#include<stdlib.h>
typedef struct SList
{
	int data;
	struct SList* next;
}SL;
void SLInit(SL** ps)
{
	*ps = (SL*)malloc(sizeof(SL));
	(*ps)->next = NULL;
}
void SLCreate(SL** ps, int n)
{
	SL* cur = *ps;
	int x = 0;
	while (n--)
	{
		scanf("%d", &x);
		getchar();
		SL* newnode = (SL*)malloc(sizeof(SL));
		newnode->data = x;
		cur->next = newnode;
		cur = newnode;
	}
	cur->next = NULL;
}
void SLCombine(SL** psA, SL** psB)
{
	SL* beginB = (*psB)->next;
	SL* endA = (*psA)->next;
	while (endA->next)//去找A链表的尾节点
	{
		endA = endA->next;
	}
	while (beginB)//B链表为空时结束
	{
		int flag = 0;//每次用flag记录一下
		SL* beginA = (*psA)->next;//每一次循环都把A放在首位
		while (beginA)
		{
			if (beginB->data == beginA->data)
			{
				flag = 1;//在A链表中找到和B相等的数
				break;
			}
			beginA = beginA->next;//beginA向后迭代
		}
		if (flag == 1)//等于1时结束程序说明是因为找到相等的数结束
		{
			beginB = beginB->next;//beginB向后迭代
		}
		else//flag=0时说明是beginA走到尾节点时结束的上面的while循环,需要把这个数链接在后面
		{
			SL* newnode = (SL*)malloc(sizeof(SL));
			newnode->data = beginB->data;
			endA->next = newnode;
			endA = newnode;
			endA->next = NULL;
			beginB = beginB->next;//beginB向后迭代
		}
	}
}
void SLPrint(SL** ps)
{
	SL* cur = (*ps)->next;
	while (cur != NULL)
	{
		printf("%d ", cur->data);
		cur = cur->next;
	}
}
int main()
{
	SL* LA=NULL;
	SLInit(&LA);
	SL* LB = NULL;
	SLInit(&LB);
	int n = 0;
	scanf("%d", &n);
	getchar();
	SLCreate(&LA, n);
	int m = 0;
	scanf("%d", &m);
	getchar();
	SLCreate(&LB, m);
	SLCombine(&LA, &LB);
	SLPrint(&LA);
	return 0;
}

目 录 一、课程设计目的 •••••••••••••••••••••••••••••••••••••••••••••••• 2 1.1、实现集合 •••••••••••••••••••••••••••••••••••••••••••••••• 2 二、课程设计内容 •••••••••••••••••••••••••••••••••••••••••••••••• 2 2.1、实现集合 •••••••••••••••••••••••••••••••••••••••••••••••• 2 三、数据结构分析 •••••••••••••••••••••••••••••••••••••••••••••••• 2 3.1、实现集合 •••••••••••••••••••••••••••••••••••••••••••••••• 2 3.1.1 集合的相等运算 •••••••••••••••••••••••••••••••••••••••• 2 3.1.2 集合的并运算 ••••••••••••••••••••••••••••••••••••••••• 3 3.1.3 集合的包含、差运算 •••••••••••••••••••••••••••••••••••• 3 四、算法分析 ••••••••••••••••••••••••••••••••••••••••••••••••••••• 3 4.1、实现集合 •••••••••••••••••••••••••••••••••••••••••••••••• 4 4.1.1 集合的相等运算 •••••••••••••••••••••••••••••••••••••••• 4 4.1.2 集合的并运算 ••••••••••••••••••••••••••••••••••••••••• 4 4.1.3 集合的包含、差运算 •••••••••••••••••••••••••••••••••••• 5 五、代码分析 ••••••••••••••••••••••••••••••••••••••••••••••••••••• 5 5.1、实现集合 •••••••••••••••••••••••••••••••••••••••••••••••• 5 5.1.1 集合的相等运算 •••••••••••••••••••••••••••••••••••••••• 5 5.1.2 集合的并运算 ••••••••••••••••••••••••••••••••••••••••• 6 5.1.3 集合的包含、差运算 •••••••••••••••••••••••••••••••••••• 7 六、问题分析 ••••••••••••••••••••••••••••••••••••••••••••••••••••• 9 6.1、实现集合 •••••••••••••••••••••••••••••••••••••••••••••••• 9 七、运行结果 ••••••••••••••••••••••••••••••••••••••••••••••••••••• 10 7.1、实现集合 •••••••••••••••••••••••••••••••••••••••••••••••• 10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

~|Bernard|

你的鼓励是我写下去最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值