两链表的并集我给修改了一下,测试通过了,你看一下我注释的地方:
#includestdio.h
#includestdlib.h
struct sted{
int num;
struct sted *next;
};
struct sted * creatlist(void)
{
int n=0;
struct sted * head,*p1,*p2;
p1=p2=(struct sted *)malloc(sizeof(struct sted));
scanf("%d",p1-num);
head=NULL;
while(p1-num)
{
n;
if(n==1)head=p1;
else p2-next=p1;
p2=p1;
p1=(struct sted *)malloc(sizeof(struct sted));
scanf("%d",p1-num);
}
p2-next=NULL;
return head;
}
void print(struct sted * head)
{
struct sted * p=head;
while(p!=NULL)
{
printf("]\n",p-num);
p=p-next;
}
return ;
}
int listlength(struct sted * head)
{
struct sted * p=head;
int m=0;
while(p!=NULL)
{
m;
p=p-next;
}
return m;
}
struct sted getEl(struct sted * head,int m)
{
struct sted e,* p=head;
int j=1;
while(pjm)
{
p=p-next;
j;
}
e=(*p);
return e;
}
int locateEl(struct sted * head,struct sted e,int n)
{//若a链表里面有和e相等的则返回第一个和e相等元素的位序,弱不相等则返回0
struct sted *p=head;
int m=1;
while(p!=NULL)
{
if(p-num!=e.num)
{
m;
p=p-next;
}
else break;
}
if(mn) m=0;
return m;
}
struct sted * listinsert(struct sted *head,int m,struct sted e)
{
//printf("Test test test\n");//调试使用 。
struct sted * q,*p=head;
q=(struct sted *)malloc(sizeof(struct sted));
*q=e;
int i;
for(i=1;im;i) p=p-next;//注意这里 。
p-next=q;
q-next=NULL;
return head;
}
struct sted * union1(struct sted * head_a,struct sted * head_b)
{
int i,Lb_len,La_len=listlength(head_a);
struct sted e, * head;
Lb_len=listlength(head_b);
for(i=1;i=Lb_len;i)
{
printf("打印取得的b链表的元素\n");
e=getEl(head_b,i);
printf("=\n",e.num);
if(!locateEl(head_a,e,La_len))
{
//La_len;
head=listinsert(head_a,La_len,e);
La_len;
}//这里的语句调过来 。
}
return head;
}
int main(void)
{
struct sted *head_a,*head_b,*head;
head_a=creatlist();//产生链表a
printf("打印a链表的内容\n");
print(head_a);//打印a链表
head_b=creatlist();//产生链表b
printf("打印b链表的内容\n");
print(head_b);//打印a链表
head=union1(head_a,head_b);//讲b链表与a链表进行并集即(a∪b)
print(head);//打印讲b链表与a链表进行并集即(a∪b)
system("pause");
return 0;
}
用c语言单链表将一个求交集的函数改成求并集的函数 , 函数如下struct Link *Union(struct Link *head1, struct Link *head2)
{
struct Link *p1 = head1-next, *p2 = head2-next;
struct Link *head, *p, *q;
head = (struct Link *)malloc(sizeof(struct Link));
head-next = NULL;
p = head;
while ((p1 != NULL)(p2 != NULL))
{
q = (struct Link*)malloc(sizeof(struct Link));
q-next = NULL;
if (p1-data =https://www.04ip.com/post/= p2-data)
{
q-data = https://www.04ip.com/post/p1-data;
p1 = p1-next;
p2 = p2-next;
}
else if (p1-datap2-data)
{
q-data = https://www.04ip.com/post/p1-data;
p1 = p1-next;
}
else
{
q-data = https://www.04ip.com/post/p2-data;
p2 = p2-next;
}
p-next = q;
p = q;
}
if(p1 != NULL){
while(p1 != NULL){
q = (struct* Link)malloc(sizeof(struct Link));
q-next = NULL;
q-data = https://www.04ip.com/post/p1-data;
p1 = p1-next;
p-next = q;
p = q;
}
}else if(p2 != NULL){
q = (struct* Link)malloc(sizeof(struct Link));
q-next = NULL;
q-data = https://www.04ip.com/post/p2-data;
p2 = p2-next;
p-next = q;
p = q;
}
return head;
}
//适用于p1,p2链表都没有重复data的情况
用c语言给定两个整数集合 a,b. 分别编写函数求这两个集合的并集(剔除重复元#includeiostream
using namespace std;
class MergeList
{
int *a;
int size;
public:
MergeList(int size,int init)
{
this-size=size;
a=new int[size];
for(int i=0;isize;i)
{
a[i]=init;
}
}
MergeList()
{
}
MergeList(const MergeListobj)
{
this-size=obj.size;
a=new int[size];
for(int i=0;isize;i)
a[i]=obj.a[i];
}
void SetData(intindex, intdata)
{
a[index]=data;
}
intGetSize(){return size;}
~MergeList()
{
delete []a;
}
MergeListoperator=(const MergeListobj)
{
this-size=obj.size;
a=new int[size];
for(int i=0;isize;i)
a[i]=obj.a[i];
return *this;
【c语言链表求并集的函数 链表归并排序c语言】 }
bool Is_Same(int element);
MergeList AddList(const MergeListobj);
void Show()
{
for(int i=0;isize;i)
couta[i]" ";
coutendl;
}
};
bool MergeList::Is_Same(int element)
{
for(int i=0;isize;i)
{
if(a[i]==element)
return true;
}
return false;
}
MergeList MergeList::AddList(const MergeListobj)
{
MergeList C;
int m=0,i=0;
C.size=size obj.size;
C.a=new int[C.size];
if(sizeobj.size||(size==obj.size))
{
for(i=0;isize;i)
{
C.a[i]=a[i];
}
}
else
{
for(i=0;iobj.size;i)
{
C.a[i]=obj.a[i];
}
}
for(int j=0;jobj.size;j)
{
if(!this-Is_Same(obj.a[j]))
{
C.a[i m]=obj.a[j];
m;
}
}
C.size=i m;
return C;
}
int main()
{
int LengthA,LengthB;
cout"input length a:";
cinLengthA;
cout"input length b:";
cinLengthB;
MergeList A(LengthA,2),B(LengthB,2),C;
cout"input A:"endl;
for(int i=0;iA.GetSize();i)
{
int a;
cina;
A.SetData(i,a);
}
cout"input B:"endl;
for(int j=0;jB.GetSize();j)
{
int b;
cinb;
B.SetData(j,b);
}
C=A.AddList(B);
cout"合并后:"endl;
C.Show();
}
c语言求并集#include stdio.h
void main()
{
int a[100],b[100],m,n,i=0,j=0,k=0,p=0;
printf("input a[],end of -1:\n");
scanf("%d",a[0]);
while(a[i]!=-1){
i;
scanf("%d",a[i]);
}
m=i;
printf("input b[],end of -1:\n");
scanf("%d",b[0]);
while(b[j]!=-1){
j;
scanf("%d",b[j]);
}
n=j;
for(j=0;jn;j){
for(i=0;im;i){
if(a[i]==b[j])
p=1;
}
if(p!=1){
a[m k]=b[j];
k;
}
p=0;
}
for(i=0;im k;i)
printf("%d ",a[i]);
}
关于c语言链表求并集的函数和链表归并排序c语言的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。
推荐阅读
- 消防工具直播视频大全,我要看消防工具
- 苹果cms资源站json接口,苹果cms官方资源采集
- 怎么上传钉钉直播视频教程,钉钉怎么发直播视频
- bmi函数c语言 bmi计算公式c语言
- 升级鸿蒙系统操作步骤q3,鸿蒙q3升级机型
- 电脑咨询语怎么消除,电脑咨询单是什么东西
- 阿里云服务器装mysql,阿里云服务器装shadoerockt 会被抓吗
- linuxsu命令错误 linux报错
- pg两个数据库连接查询,pg两个数据库连接查询不一致