集合的并、交和差运算

在以单链表表示的正整数的有序集合上,实现集合的并、交和差运算。
基本要求:
(1)用单链表存储集合中的数据;
(2)对单链表中的数据进行排序。
测试数据要求:

数据中包含的整数,是随机产生的两位整数

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

typedef struct LNode{
	int data;
	struct LNode *next;
}LNode,*LinkList;

void Creatlist(LinkList &L)
{//集合的创建
	LinkList p,pre;
	int i,len;
	int flag[100]={0};
	printf("输入产生元素的个数:\n");
	scanf("%d",&len);
	srand(time(0));
	L=(LinkList)malloc(sizeof(LNode));
	pre=L;
	for(i=0;i<len;i++)
	{
		p=(LinkList)malloc(sizeof(LNode));
		while(1)
        {
            p->data=rand()%89+10;
            if(!flag[p->data])
            {
                flag[p->data]=1;
                break;
            }
        }
        pre->next=p;
        pre=p;
	}
	pre->next=NULL;
}

void Display(LinkList &L)
{//输出数据 
	LinkList p;
    p=L->next;
    if (!p)
    {
        printf("ERROR!");
    }
    else
    {
    while(p)
    {
        if (p->data!=0)
        printf("%d ",p->data);
        p=p->next;
    }
    }
    printf("\n");
}

void Insert(LinkList &L,int m)
{//插入 
	LinkList p,r;
	r=L;
	while(r->next && r->next->data<m)
		r=r->next;
	p=(LinkList)malloc(sizeof(LNode));
	p->data=m;
	p->next=r->next;
	r->next=p;
}

void Sort(LinkList &L)
{//对集合进行排序
	LinkList q,r;
	q=L->next;
	L->next=NULL;
	while(q)
	{
		Insert(L,q->data);
		r=q;
		q=q->next;
		free(r);
	}
}

void Merge(LinkList La,LinkList Lb,LinkList &Lc)
{//求集合的并集
	LinkList p,q;
	Sort(La);
	Sort(Lb);
	p=La->next;
	q=Lb->next;
	Lc=(LinkList)malloc(sizeof(LNode));
	Lc->next=NULL;
	while(p && q)
	{
		if(p->data<q->data)
		{
			Insert(Lc,p->data);
			p=p->next;
		}
		else if(p->data>q->data)
		{
			Insert(Lc,q->data);
			q=q->next;
		}
		else
		{
			Insert(Lc,p->data);
			p=p->next;
			q=q->next;
		}
	}
	while(p)
	{
		Insert(Lc,p->data);
		p=p->next;
	}
	while(q)
	{
		Insert(Lc,q->data);
		q=q->next;
	}
	printf("两个集合的并集是:\n");
	Display(Lc);
}

void InterList(LinkList La,LinkList Lb,LinkList &Lc)
{//求两个集合的交集
	LinkList cur1,cur2,cur;
    Lc=(LinkList)malloc(sizeof(LNode));
    Lc->next=NULL;
    cur1=La->next;
    cur2=Lb->next;
    cur=Lc;
    while(cur1 && cur2)
    {
        if(cur1->data>cur2->data)
            cur2=cur2->next;
        else if(cur1->data<cur2->data)
            cur1=cur1->next;
        else
        {
            cur->next=(LinkList)malloc(sizeof(LNode));
            cur=cur->next;
            cur->next=NULL;
            cur->data=cur2->data;
            cur1=cur1->next;
            cur2=cur2->next;
        }
    }
    cur->next=NULL;
	if(Lc->next==NULL)
		printf("两个集合交集为空。\n");
	else
	{
		printf("两个集合交集为:\n");
		Display(Lc);
	}
}

void Diffset(LinkList La,LinkList Lb,LinkList &Lc)
{//求两个集合的差集
    LinkList pa,pb,pc,s;
    Lc=La;
    pa=La->next;pb=Lb->next;pc=Lc;
    while(pa&&pb)
    {
        if(pa->data==pb->data)
        {
            s=pa;pa=pa->next;free(s);
            while(pa&&(pa->data==pb->data))
            {
                s=pa;pa=pa->next;free(s);
            }
        }
        else if(pa->data<pb->data)
        {
            pc->next=pa;pc=pa;pa=pa->next;
        }
        else
        {
            s=pb;pb=pb->next;free(s);
        }
    }
    if(!pa)
    {
        while(pb)
        {
            s=pb;pb=pb->next;free(s);
        }
        pc->next=0;
    }
    else
        pc->next=pa;
    free(Lb);
    printf("两个集合的差集是:\n");
    Display(La);
}

int main()
{
	LinkList La,Lb,Lc;
	Lc=NULL;
	printf("建立第一个集合,");
	Creatlist(La);
    printf("第一个集合排序后的结果为:\n");
    Sort(La);
	Display(La);
	printf("\n");
    printf("建立第二个集合,");
    Creatlist(Lb);
	printf("第二个集合排序后的结果为:\n");
    Sort(Lb);
	Display(Lb);
	printf("\n");
	printf("--------对两个集合进行操作------\n");
	printf("\n");
	Merge(La,Lb,Lc);
	InterList(La,Lb,Lc);
    Diffset(La,Lb,Lc);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值