输入格式:
输入两行正整数,每行均以0结束(0之前的整数为线性表中数据元素,不包括0)。
输出格式:
输出两个线性表中交集,每个整数之间以一个空格隔开;如果两个线性表没有交集则输出“empty class”。
输入样例:
1 5 9 11 15 0
2 4 5 6 9 10 0
输出样例:
5 9
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
int main()
{
struct node *head1,*p,*q;
head1=NULL;
int a;
scanf("%d",&a);
while(a!=0)
{
p=(struct node *)malloc(sizeof(struct node*));
p->data=a;
p->next=NULL;
if(head1==NULL) head1=p;
else q->next=p;
q=p;
getchar();
scanf("%d",&a);
}
//getchar();
struct node *head2,*m,*n;
head2=NULL;
scanf("%d",&a);
while(a!=0)
{
m=(struct node *)malloc(sizeof(struct node*));
m->data=a;
m->next=NULL;
if(head2==NULL) head2=m;
else n->next=m;
n=m;
getchar();
scanf("%d",&a);
}
//完成两个链表的输入
struct node *x,*y;
x=head1;
y=head2;
int flag=0;
while(x!=NULL)
{
while(y!=NULL)
{
if(x->data==y->data)
{
flag=1;
printf("%d ",x->data);
break;
}
else y=y->next;
}
x=x->next;
y=head2;
}
if(flag==0) printf("empty class");
return 0;
}
我不喜欢题目中给的代码所以我按照自己的想法重新写了一段代码