题目:
已知三个带头结点的线性链表A、B和C中的结点均依元素值自小至大非递减排列(可能存在两个以上值相同的结点)。
编写算法对A表进行如下操作:
使操作后的链表A中仅留下三个表中均包含的数据元素的结点,且没有值相同的结点,并释放所有无用结点。限定算法的时间复杂度为O(m+n+p),其中m、n和p分别为三个表的长度。
看代码:由于我将方法都封装到了类里面,所以第一个链表的头结点就在类中声明了,所以在参数中只有ha和hb
void mySLink::sim_non_repeat(PHEAD ha, PHEAD hb)
{
PNODE head = pHead->next;
ha = ha->next;
hb = hb->next;
while (head)
{
/*去掉重复结点*/
if (head->next && head->data == head->next->data)
{
PNODE temp = head;
getPrior(head)->next=head->next;
head = head->next;
delete temp;
continue;
}
/**/
while (ha && ha->data < head->data)
{
ha = ha->next;
}
while (hb && hb->data < head->data)
{
hb = hb->next;
}
if (ha && hb && ha->data == head->data && hb->data == head->data)
{
head = head->next;
}
else
{
if (head)
{
PNODE temp = head;
getPrior(head)->next = head->next;
head = head->next;
delete temp;
}
else
{
PNODE temp = head;
getPrior(head)->next =NULL;
delete temp;
}
}
}
}