typedef struct ListNode
{
DataType data;
ListNode* next;
}ListNode;
//交集(注意:list1和list2如果各有2个4,交集也会出现两次)
ListNode* Intersection(ListNode* plist1, ListNode* plist2)
{
//判断不为空
if(plist1 == NULL || plist2 == NULL)
{
return NULL;
}
//头结点(哨兵位节点)
ListNode* newlist = BuyNode(0);
ListNode* tail = newlist;
//相等了一起走,值给tail,不相等小的走
while(plist1 && plist2)
{
if(plist1->data == plist2->data)
{
tail->next = plist1;
tail = plist1;
plist1 = plist1->next;
plist2 = plist2->next;
}
else if(plist1->data < plist2->data)
{
plist1 = plist1->next;
}
else
{
plist2 = plist2->next;
}
}
tail->next = NULL; //注意tail->next要置空
return newlist->next;
}
//差集 ListNode* DifSet(ListNode* plist1, ListNode* plist2) { //判断不为空 if(plist1 == NULL || plist2 == NULL) { return NULL; } //头结点(哨兵位节点) ListNode* newlist = BuyNode(0); ListNode* tail = newlist; while(plist1 && plist2) { //相等一起走 if(plist1->data == plist2->data) { plist1 = plist1->next; plist2 = plist2->next; }
else if(plist1->data < plist2->data) { tail->next = plist1; tail = plist1; plist1 = plist1->next; } else { tail->next = plist2; tail = plist2; plist2 = plist2->next; } } if(plist1) { tail->next = plist1; } else { tail->next = plist2; } return newlist->next; }