如何获得两套在链表中共同的元素? [英] How to get the common elements of the two sets in a Linked List?
本文介绍了如何获得两套在链表中共同的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有2链表,我想比较两个列表并打印出现在两个列表中的每个常见的元素。想尽一切无法得到它的工作。
结构节点* calcIntersection(结构节点* headA,结构节点* headB)
{ 结构节点*链接1 = headA; 结构节点*链接2 = headB; 而(LINK1!= NULL)
{ 如果(link2->价值== link2->值)
{ 的printf(%d个,link1->值); } 链接1 =链路1 - > pNext; } 返回链接1;}
解决方案
您比较接近,在现实中你的方法应该是这样的:
结构节点* calcIntersection(结构节点* headA,结构节点* headB)
{
结构节点*链接1 = headA; 而(LINK1!= NULL)
{
结构节点*链接2 = headB; 而(LINK2!= NULL)
{
如果(link1->价值== link2->值)
{
的printf(%d个,link1->值);
} 链接2 = link2-> pNext;
} 链接1 = link1-> pNext;
} 返回链接1;
}
我不太知道你正在返回那里,但你会被返回NULL大多数情况下,我不太清楚,如果这就是无论你想要什么。
I have got 2 linkedlist, I want to compare both lists and print every common element that appears in both list. tried everything can't get it to work.
struct Node *calcIntersection(struct Node *headA, struct Node *headB)
{
struct Node * link1 = headA;
struct Node * link2 = headB;
while(link1 != NULL)
{
if (link2->value == link2->value)
{
printf("%d", link1->value);
}
link1 = link1 -> pNext;
}
return link1;
}
解决方案
You are close, in reality your method should look like this:
struct Node *calcIntersection(struct Node *headA, struct Node *headB)
{
struct Node *link1 = headA;
while(link1 != NULL)
{
struct Node *link2 = headB;
while (link2 != NULL)
{
if (link1->value == link2->value)
{
printf("%d", link1->value);
}
link2 = link2->pNext;
}
link1 = link1->pNext;
}
return link1;
}
I'm not quite sure what you are returning there, but in most situations you will be returning NULL, I'm not quite sure if that's what you want however.
这篇关于如何获得两套在链表中共同的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文