已知A,B和C为三个递增有序的线性表,现要求对A表作如下操作:删除那些既在B表中出现又在C表中出现的元素。
下面的代码有问题吗?
typedef int DataType; /数据类型/
typedef int ElemType; /元素类型/
//----------------线性列表存储结构----------------
typedef struct LNode
{
ElemType data;
struct LNode next;
}LNode, LinkList;
void ListCreate(LinkList L, int n)
//创建含有n个结点的链表
{
int i;
LinkList p, q;
p = L;
for(i = 0 ; i < n; i++)
{
q = (LinkList)malloc(sizeof(LNode)); //生成新结点
scanf("%d", &q->data);
q->next = NULL;
p->next = q; //前后结点链接
p = q;
}
}
void ListMerge(LinkList La, LinkList Lb, LinkList Lc)
{
LinkList pa = La->next, pb = Lb->next;
LinkList pc = Lc;
while(pa && pb)
{
if(pa->data <= pb->data)
{
pc ->next = pa;
pc = pa;
pa = pa->next;
}
else
{
pc->next = pb;
pc = pb;
pb = pb->next;
}
}
pc->next = pa?pa:pb; //插入剩余段
free(La);
free(Lb);
}
void ListTraverse(LinkList L)
{
LinkList p = L;
while(p->next)
{
p = p->next;
printf("%d\n", p->data);
}
}
int main()
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
LNode La, Lb, Lc;
ListCreate(&La, a);
ListCreate(&Lb, b);
ListCreate(&Lc, c);
LinkList pa, pb, pc, pre;
pa = La.next, pb = Lb.next, pc = Lc.next;
pre = &La;
//遍历pa,删除三个表中相同的结点
while(pa)
{
//分别在Lb和Lc中找到第一个大于等于pa的结点
while(pc && pb &&(pb->data<pa->data || pc->data<pa->data))
{
if(pb->data < pa->data)
pb = pb->next;
if(pc->data<pa->data)
pc = pc->next;
}
//Lb或Lc遍历完,停止遍历pa
if(!pb || !pc)
break;
if(pb->data == pa->data && pc->data == pa->data)
{
LinkList temp = pa;
//pre所指结点的指针向后移动,pre指针本身不移动
pre->next = pa->next;
pa = pa->next;
free(temp);
}
else
{
pre = pa;
pa = pa->next;
}
}
ListTraverse(&La);
return 0;
}
有没有问题,运行一下不就知道了