解决方法:
遍历list1,若list1中的数据在list2中也出现了,那么删除list1中的该数据,否则保留。
代码实现:
typedef struct Node
{
int data;
struct Node* next;
}Node,*PNode;
void Difference(PNode* pHead1, PNode pHead2)
{
PNode pList1 = *pHead1;
PNode pList2 = NULL;
PNode pTail = NULL;//记录pHead1中的尾结点,以便链接符合条件的结点
PNode pTmp = NULL;//记录pHead2中不在pHead1中的结点,以便删除
while (pList1)
{
pList2 = pHead2;
while (pList2 && pList1->data != pList2->data)
pList2 = pList2->next;
if (pList2)//pList2不存在或pList2存在且pList1 == pList2
{
if (pTail == NULL)
*pHead1 = pList1->next;
else
pTail->next = pList1->next;
pTmp = pList1;
pList1 = pList1->next;
free(pTmp);
}
else
{
pTail = pList1;
pList1 = pList1->next;
}
}
}