7-1 顺序表求交集

输入格式:

输入两行正整数,每行均以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;
}

我不喜欢题目中给的代码所以我按照自己的想法重新写了一段代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值