题目
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/43548a40caefd870c74b483bc13bfdf6.png)
答案
#include<stdio.h>
#include<malloc.h>
struct Array{
int data;
struct Array *next;
};
int main()
{
struct Array *head,*head1,*pa,*pb,*head2,*p,*temp;
head1=(struct Array *)malloc(sizeof(struct Array));
head1->next=NULL;
p=head1;
int t;
while(1)
{
scanf("%d",&t);
if(t==-1) break;
else
{
temp=(struct Array *)malloc(sizeof(struct Array));
temp->data=t;
temp->next=NULL;
p->next=temp;
p=p->next;
}
}
head2=(struct Array *)malloc(sizeof(struct Array));
head2->next=NULL;
p=head2;
while(1)
{
scanf("%d",&t);
if(t==-1) break;
else
{
temp=(struct Array *)malloc(sizeof(struct Array));
temp->data=t;
temp->next=NULL;
p->next=temp;
p=p->next;
}
}
head=(struct Array *)malloc(sizeof(struct Array));
head->next=NULL;
p=head;
pa=head1->next;
pb=head2->next;
while(pa&&pb)
{
if(pa->data==pb->data)
{
p->next=pa;
p=p->next;
pa=pa->next;
pb=pb->next;
}
else if(pa->data<pb->data)
pa=pa->next;
else if(pa->data>pb->data)
pb=pb->next;
}
p->next=NULL;
int flag=0;
if(head->next==NULL)
{
printf("NULL");
return 0;
}
while(head->next)
{
if(flag==0)
{
printf("%d",head->next->data);
flag=1;
}
else printf(" %d",head->next->data);
head=head->next;
}
}