...存储一个集合,设计程序实现两个集合的交集运算A=A∩B。
发布网友
发布时间:2024-10-23 15:27
我来回答
共1个回答
热心网友
时间:2024-11-13 22:20
//伪代码
struct node *pA = pListAHead; //有序列表A
struct node *pB = pListBHead; //有序列表B
struct node *pCHead = NULL; //A与B交集头
struct node *pCEnd = NULL; //A与B交集尾
while (pA != NULL && pB != NULL)
{
if (pA->value == pB->value)
{
//相等,同时后移
if (pCEnd == NULL)
{
pCHead = new node();
pCHead->value = pA->value;
pCHead->next = NULL;
pCEnd = pCHead;
}
else
{
pCEnd->next = new node();
pCEnd = pCEnd->next;
pCEnd->next = NULL;
pCEnd->value = pA->value;
}
pA = pA->next;
pB = pB->next;
continue;
}
else if (pA->value > pB->value)
{
//最小的相比,B小 B后移
pB = pB->next;
continue;
}
else
{
//最小的相比,A小 A后移
pA = pA->next;
}
}