已知递增链表A,B分别存储了一个集合。设计算法实现求两个集合的交集,要代码
3个回答
展开全部
# #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");
# }
# #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");
# }
参考资料: http://blog.csdn.net/albert32/article/details/6797869
展开全部
递增无序?递增有序吧,如果无序,首先给两个链表排序,以下代码按升序合并{ tmp = p2->next;/* 保存结点p2的下一个结点 */ free(p2); /*
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询