Time Limit: 3000ms, Memory Limit: 10000KB, Accepted: 299, Total Submissions: 494
Description
已知A,B和C为三个非递减有序的线性表,均以单链表作为存储结构。现要求对A表作如下操作:删去那些既在B表中出现又在C表中出现的元素。试对单链表编写实现上述操作的算法,并释放A表中的无用结点空间。
Input
第一行输入3个正整数m,n,p(m,n,p<=100),用空格分开,表示三个线性表中的元素个数,其后3行依次输入A,B,C表中的元素。
Output
输出实现上述操作后的A表。
- Sample Input
8 5 6 1 2 3 4 5 6 6 7 2 3 5 9 12 2 4 5 6 12 13
- Sample Output
1 3 4 6 6 7
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int num;
struct node *next;
}node;
int a, b, c, step=0;
node* creat(int n)
{
node *p, *q, *head;
head = (node* )malloc(sizeof(node));
p = head;
for(int i = 0; i < n; i++)
{
q = (node* )malloc(sizeof(node));
scanf("%d",&(q->num));
p->next = q;
p = q;
}
p->next = NULL;
return head;
}
node* mix(node* list2, node* list3)
{
node *p, *q, *head, *m, *l;
head = (node* )malloc(sizeof(node));
l = head;
p = list2;
q = list3;
m = q;
for(int i = 0; i < b; i++)
{
p = p->next;
for(int j = 0; j < c; j++)
{
m = m->next;
if(p->num == m->num)
{
l->next = p;
l = p;
step++;
break;
}
}
m = q;
}
l->next = NULL;
return head;
}
node* del(node* head, node* list)
{
node *p, *q, *m, *n;
p = head;
q = list;
m = p;
n = q->next;
while(n->next)
{
for(int i = 0; i < step; i++)
{
m = m->next;
if(n->num == m->num)
{
q->next = n->next;
free(n);
break;
}
}
m = p;
q = q->next;
n = q->next;
}
for(int i = 0;i < step;i++)
{
m = m->next;
if(n->num == m->num)
{
free(n);
q->next = NULL;
break;
}
}
return list;
}
node* getout(node* head)
{
node *p;
p = head->next;
while(p)
{
if(p->next == 0)
{
printf("%d\n",p->num);
break;
}
else
{
printf("%d ",p->num);
p = p->next;
}
}
return 0;
}
int main()
{
scanf("%d%d%d",&a,&b,&c);
node *list1, *list2, *list3, *head,*p;
list1 = creat(a);
list2 = creat(b);
list3 = creat(c);
head = mix(list2, list3);
p=del(head, list1);
getout(p);
return 0;
}