...存储了一个集合。设计算法实现求两个集合的交集,要代码

发布网友 发布时间:2024-10-23 15:27

我来回答

3个回答

热心网友 时间:2024-11-14 04:04

# #include <stdio.h>
# #include<stdlib.h>
#
# typedef struct node
# {
# int data;
# struct node* next;
# }Link;
#
# //链表初始化 必须建立递增的链表
# Link* CreateLa(Link *head,int n)
# {
# Link *p;
# //头插法
# //for(int i=0;i<n;i++)
# //{
# // p = (Link *)malloc(sizeof(Link));
# // p->data = 2*i;
# // p->next = head->next;
# // head->next = p;
# //}
# //尾插法
# for(int i=0;i<n;i++)
# {
# p = (Link *)malloc(sizeof(Link));
# p->data = 2*i;
# head->next = p;
# head = p;
#
# }
# head->next = NULL;
# return head;
# }
#
# Link* CreateLb(Link *head,int n)
# {
# Link *p;
# //尾插法
# for(int i=0;i<n;i++)
# {
# p = (Link *)malloc(sizeof(Link));
# p->data = i+1;
# head->next = p;
# head = p;
#
# }
# head->next = NULL;
# return head;
# }
# void Print(Link *head)
# {
# Link *p = head->next;
# while(p!=NULL)
# {
# printf("%d\t",p->data);
# p = p->next;
# }
# }
#
# Link *Intersection(Link *La,Link *Lb,Link *Lc)
# {
# Link *pa,*pb,*pc,*p;
# pc = Lc;
# pa = La->next;
# pb = Lb->next;
# while(pa!=NULL&&pb!=NULL)
# {
# if(pa->data<pb->data)
# {
# pa = pa->next;
# }
# if(pa->data>pb->data)
# {
# pb = pb->next;
# }
# else
# {
# p = (Link *)malloc(sizeof(Link));
# p->data = pa->data;
# pc->next = p;
# pc = p;
# pa = pa->next;
# pb = pb->next;
# }
# }
# pc->next = NULL;
# return Lc;
# }
# int main()
# {
# int a,b;
# Link *La,*Lb,*Lc;
# La = (Link *)malloc(sizeof(Link));
# Lb = (Link *)malloc(sizeof(Link));
# Lc = (Link *)malloc(sizeof(Link));
# La->next = NULL;
# Lb->next = NULL;
# Lc->next = NULL;
# printf("请输入链表a的数目:");
# scanf("%d",&a);
# printf("\n请输入链表b的数目:");
# scanf("%d",&b);
# CreateLa(La,a);
# printf("链表la为:\n");
# Print(La);
# CreateLb(Lb,b);
# printf("\n链表lb为:\n");
# Print(Lb);
# Intersection(La,Lb,Lc);
# printf("\n链表lc为:\n");
# Print(Lc);
# puts("\n");
# system("pause");
# }

热心网友 时间:2024-11-14 04:08

递增无序?递增有序吧,如果无序,首先给两个链表排序,以下代码按升序合并{ tmp = p2->next;/* 保存结点p2的下一个结点 */ free(p2); /*

热心网友 时间:2024-11-14 04:02

http://zhidao.baidu.com/question/3507477.html

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com