单链表集合求交集

用单链表标示集合,设计一个算法求两个结合的交集。

#include "stdafx.h"
#include "math.h"

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

//求LA和LB的交集LC
void interSet(LinkList *LA,LinkList *LB, LinkList *&LC)
{
	//a的移动节点ta b的移动节点tb c的移动节点tc c的尾节点rc
	LinkList *ta = LA->next, *tb,*tc,*rc;
	//建立c头
	LC = new LinkList();
	rc = LC;
	while (ta)
	{
		tb = LB ->next;
		while (tb && tb -> data != ta ->data)
			tb = tb ->next;
		if(tb)
		{
			tc = new LinkList();
			tc ->data = ta ->data; //仅复制数据
			rc->next = tc;//添加到c
			rc = tc;//移动尾节点
		}
		ta = ta ->next;
	}

	rc ->next = 0;
}

void createLinkList(LinkList *&LA, ElemType data[], int n)
{
	LA = new LinkList();
	LinkList *ra = LA, *ta;
	for (int idx= 0; idx < n; idx++)
	{
		ta = new LinkList();
		ta ->data = data[idx];
		ra -> next = ta;
		ra = ta;
	}
	ra = 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
	LinkList *LA, *LB, *LC;

	ElemType dataA[] = {5,4,6,1,8,3};
	ElemType dataB[] = {8,5,9,12,4,7,6,3};

	createLinkList(LA, dataA, 6);
	createLinkList(LB, dataB, 8);

	interSet(LA, LB, LC);

	LinkList *tc = LC ->next;
	while (tc)
	{
		printf("%4d", tc->data);
		tc = tc -> next;
	}

	return 0;
}


运行结果:


ps:算法时间复杂度 O(n2), 想优化一下时间复杂度。想的结果是先让a简历一个二叉树,再比较B的元素。然后想为何答案也是n2, 应该是没有到二叉树的知识吧。


  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
假设我们有两个单链表A和B,它们分别表示两个集合。为了它们的并集,我们可以遍历链表A和B,并将链表B中不在链表A中的元素插入到链表A的末尾。具体步骤如下: 1. 遍历链表A,对于每一个节点,检查该节点对应的元素是否在链表B中出现过,如果出现过,则跳过该节点;如果没有出现过,则将该节点插入到链表B的末尾。 2. 返回链表A,此时链表A中包含了两个集合的并集。 下面是C++代码实现: ```c++ #include <iostream> #include <unordered_set> using namespace std; struct ListNode { int val; ListNode* next; ListNode(int x) : val(x), next(NULL) {} }; ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) { if (!headA || !headB) { return NULL; } // 将链表A中的元素插入到哈希表中 unordered_set<int> hashset; ListNode* p = headA; while (p) { hashset.insert(p->val); p = p->next; } // 遍历链表B,将不在哈希表中的元素插入到链表A的末尾 p = headB; while (p) { if (hashset.find(p->val) == hashset.end()) { ListNode* node = new ListNode(p->val); node->next = headA; headA = node; } p = p->next; } return headA; } int main() { // 构造两个集合单链表表示 ListNode* headA = new ListNode(1); headA->next = new ListNode(2); headA->next->next = new ListNode(3); headA->next->next->next = new ListNode(4); headA->next->next->next->next = new ListNode(5); ListNode* headB = new ListNode(4); headB->next = new ListNode(5); headB->next->next = new ListNode(6); headB->next->next->next = new ListNode(7); // 两个集合的并集 ListNode* head = getIntersectionNode(headA, headB); // 输出结果 while (head) { cout << head->val << " "; head = head->next; } cout << endl; return 0; } ``` 注意,上述代码中的getIntersectionNode函数实际上是两个集合交集,如果将其改为并集,只需要将第二个while循环中的if条件语句改为hashset.find(p->val) != hashset.end()即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

当当小螳螂

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值