7-3 集合的运算-并、交、对称差

![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/e1985d34e5ea45a5ae6c0bfd3c85c227.png

顺序表:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int *p=new int[n];
    for(int i=0;i<n;i++)
        cin>>p[i];
    int m;
    cin>>m;
    int *q=new int [m];
    for(int j=0;j<m;j++)
        cin>>q[j];
    int *bing=new int[n+m];
    int *jiao = new int [n+m];
    int *cha = new int [n+m];
    for(int i=0;i<n;i++)
    {
        bing[i]=p[i];
    }
    int cnt_bing=n,cnt_jiao=0,cnt_cha=0;  //记录并集,交集,差集的个数
    for(int j=0;j<m;j++)
    {
        bool b_bing=1;			//判断是否要往并集添加元素
        for(int i=0;i<n;i++)
        {
            if(p[i]==q[j])
            {
                b_bing=0;
                jiao[cnt_jiao++]=p[i];
                break;
            }
        }
        if(b_bing)
        {
            bing[cnt_bing++]=q[j];
        }
    }
    for(int ii=0;ii<n;ii++)			//从集合1中删去交集
    {
        bool b_cha=1;
        for(int jj=0;jj<cnt_jiao;jj++)
        {
            if(p[ii]==jiao[jj])
            {
                b_cha=0;
                break;
            }
        }
        if(b_cha)
        {
            cha[cnt_cha++]=p[ii];
        }
    }
    for(int ii=0;ii<m;ii++)		//从集合2中删去交集
    {
        bool b_cha=1;
        for(int jj=0;jj<cnt_jiao;jj++)
        {
            if(q[ii]==jiao[jj])
            {
                b_cha=0;
                break;
            }
        }
        if(b_cha)
        {
            cha[cnt_cha++]=q[ii];
        }
    }
    for(int i=0;i<cnt_bing;i++)
    {
        cout<<bing[i]<<" ";
    }
    if(cnt_bing==0)cout<<"NULL";
    cout<<"\n";
    for(int i=0;i<cnt_jiao;i++)
    {
        cout<<jiao[i]<<" ";
    }
    if(cnt_jiao==0)cout<<"NULL";
    cout<<"\n";
    for(int i=0;i<cnt_cha;i++)
    {
        cout<<cha[i]<<" ";
    }
    if(cnt_cha==0)cout<<"NULL";



    return 0;
}

队列

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n,m;
    cin>>n;
    int *p=new int [n];
    for(int i=0;i<n;i++)
    {
        cin>>p[i];
    }
    cin>>m;
    int *q=new int [m];
    for(int i=0;i<m;i++)
    {
        cin>>q[i];
    }
    int *bing=new int[n+m];
    int *jiao = new int [n+m];
    int *cha = new int [n+m];
    int h_bing=0,t_bing=-1,h_jiao=0,t_jiao=-1,h_cha=0,t_cha=-1;	//并集,交集,差集的头指针与尾指针
    for(int i=0;i<n;i++)
    {
        t_bing++;
        bing[t_bing]=p[i];
    }
    for(int j=0;j<m;j++)
    {
        bool b_bing=1;
        for(int i=0;i<n;i++)
        {
            if(p[i]==q[j])
            {
                b_bing=0;
                t_jiao++;
                jiao[t_jiao]=p[i];
                break;
            }
        }
        if(b_bing)
        {
            t_bing++;
            bing[t_bing]=q[j];
        }
    }
    for(int i=0;i<n;i++)
    {
        bool b_cha=1;
        h_jiao=0;
        while (h_jiao<=t_jiao)
        {
            if(p[i]==jiao[h_jiao])
            {
                b_cha=0;
                break;
            }
            h_jiao++;		//弹出交集的头元素
        }
        if(b_cha)
        {
            t_cha++;
            cha[t_cha]=p[i];
        }
    }
    for(int i=0;i<m;i++)
    {
        bool b_cha=1;
        h_jiao=0;
        while (h_jiao<=t_jiao)
        {
            if(q[i]==jiao[h_jiao])
            {
                b_cha=0;
                break;
            }
            h_jiao++;		//弹出交集的头元素
        }
        if(b_cha)
        {
            t_cha++;
            cha[t_cha]=q[i];
        }
    }
    h_jiao=0;  //让交集的头指针重新回到0
    while (h_bing<=t_bing)
    {
        cout<<bing[h_bing]<<" ";
        h_bing++;
    }
    if(t_bing==-1)cout<<"NULL";
    cout<<"\n";
    while (h_jiao<=t_jiao)
    {
        cout<<jiao[h_jiao]<<" ";
        h_jiao++;
    }
    if(t_jiao==-1)cout<<"NULL";
    cout<<"\n";
    while (h_cha<=t_cha)
    {
        cout<<cha[h_cha]<<" ";
        h_cha++;
    }
    if(t_cha==-1)cout<<"NULL";
    return 0;
}

链表:

#include <bits/stdc++.h>
using namespace std;
struct node{
    int num;
    node *next;
};
int main()
{
    int n,m;
    cin>>n;
    node*head_1=new node,*head_2=new node,*temp,*tail;
    head_1->next= nullptr;  //置零
    head_2->next= nullptr;	//置零
    tail=head_1;			//让tail指向第一个集合的头
    for(int i=0;i<n;i++)
    {
        temp=new node;			//开辟新结点
        int num;
        cin>>num;
        temp->num=num;
        temp->next= nullptr;
        tail->next=temp;

        tail=tail->next;
    }
    cin>>m;
    tail=head_2;		//让tail指向第2个集合的头
    for(int i=0;i<m;i++)
    {
        temp=new node;		//开辟新结点
        int num;
        cin>>num;
        temp->num=num;
        temp->next= nullptr;
        tail->next=temp;
        tail=tail->next;
    }
    node*head_b=new node,*head_j=new node,*head_c=new node;
    head_b->next= nullptr;head_j->next= nullptr;head_c->next= nullptr;		//置零
    tail=head_b;			//现在让tail指向并集的头,要往并集添加元素
    temp=head_1;			//temp没有用了,所以让temp指向第一个集合的头
    for(int i=0;i<n;i++)
    {
        temp=temp->next;
        node *p=new node;
        p->num=temp->num;
        p->next= nullptr;
        tail->next=p;
        tail=tail->next;

    }
    //现在tail指针是并集的尾指针
    temp=head_2;  //temp指向第二个集合的头指针
    node*tail_j=head_j;     //创建新指针指向交集的头。
    for(int j=0;j<m;j++)
    {
        bool b_bing=1;
        node *p=head_1; //创建新指针指向第一个集合的头
        temp=temp->next;
        for(int i=0;i<n;i++)
        {
            p=p->next;
            if(temp->num==p->num)
            {
                b_bing=0;
                node *p_new=new node;   //开辟新结点
                p_new->num=temp->num;
                p_new->next= nullptr;
                tail_j->next=p_new;    //连接到交集
                tail_j=tail_j->next;
                break;
            }
        }
        if(b_bing)
        {
            node *p_new=new node;  //开辟新结点
            p_new->num=temp->num;
            p_new->next=nullptr;
            tail->next=p_new;           //让并集连接上新的数
            tail=tail->next;
        }
    }
    node *tail_c=head_c;    //创建新指针指向差集的头。
    temp=head_1;        //让temp现在指向第一个集合的头。
    for(int i=0;i<n;i++)
    {
        bool b_cha=1;
        temp=temp->next;
        node *temp_j=head_j->next; //创建新指针指向交集的第一个数据。
        while (temp_j!= nullptr)
        {
            if(temp->num==temp_j->num)
            {
                b_cha=0;
                break;
            }
            temp_j=temp_j->next;
        }
        if(b_cha)
        {
            node *p_new=new node;
            p_new->num=temp->num;
            p_new->next= nullptr;
            tail_c->next=p_new;
            tail_c=tail_c->next;
        }
    }
    temp=head_2;        //让temp现在指向第2个集合的头。
    for(int i=0;i<m;i++)
    {
        bool b_cha=1;
        temp=temp->next;
        node *temp_j=head_j->next; //创建新指针指向交集的第一个数据。
        while (temp_j!= nullptr)
        {
            if(temp->num==temp_j->num)
            {
                b_cha=0;
                break;
            }
            temp_j=temp_j->next;
        }
        if(b_cha)
        {
            node *p_new=new node;
            p_new->num=temp->num;
            p_new->next= nullptr;
            tail_c->next=p_new;
            tail_c=tail_c->next;
        }
    }

    temp=head_b->next;  //现在让temp指向并集的第一个数据集
    while (temp!= nullptr)
    {
        cout<<temp->num<<" ";
        temp=temp->next;
    }
    if(head_b->next== nullptr)cout<<"NULL"; //如果并集为空
    cout<<"\n";

    temp=head_j->next;  //现在让temp指向交集的第一个数据集
    while (temp!= nullptr)
    {
        cout<<temp->num<<" ";
        temp=temp->next;
    }
    if(head_j->next== nullptr)cout<<"NULL"; //如果交集为空
    cout<<"\n";

    temp=head_c->next;  //现在让temp指向差集的第一个数据集
    while (temp!= nullptr)
    {
        cout<<temp->num<<" ";
        temp=temp->next;
    }
    if(head_c->next== nullptr)cout<<"NULL"; //如果交集为空
}

在C语言中,标准库并没有直接提供集合运算如并集(union)、集(intersection)和对称(symmetric difference)的操作。然而,你可以通过数组或结构体来模拟这些操作,因为C本身并不支持内置的数据结构。 下面是一个简单的示例,使用数组来实现基本的集合概念: ```c #include <stdio.h> #include <stdlib.h> // 定义集合元素 typedef int Element; // 集合的结构体 typedef struct Set { Element *elements; // 存储元素的数组 size_t count; // 集合中的元素个数 } Set; // 创建集合 Set* create_set(Element *elts, size_t len) { Set *set = malloc(sizeof(Set)); set->elements = elts; set->count = len; return set; } // 添加元素到集合 void add_to_set(Set **set, Element e) { if (*set == NULL || (*set)->count == sizeof(*set->elements)) { // 扩展数组大小 (*set)->elements = realloc((*set)->elements, ((*set)->count + 1) * sizeof(Element)); } (*set)->elements[(*set)->count++] = e; } // 合并两个集合 Set* union_sets(Set *set1, Set *set2) { Set *union_set = create_set(NULL, 0); for (size_t i = 0; i < set1->count; i++) { add_to_set(&union_set, set1->elements[i]); } for (size_t i = 0; i < set2->count; i++) { add_to_set(&union_set, set2->elements[i]); } return union_set; } // 集操作(这里仅返回元素相同的部分,未实现) Set* intersection_sets(Set *set1, Set *set2) { Set *result = create_set(NULL, 0); // 实现略 return result; } // 对称(这里仅返回不属于其他集合的元素,未实现) Set* symmetric_difference_sets(Set *set1, Set *set2) { Set *result = create_set(NULL, 0); // 实现略 return result; } // 释放集合内存 void destroy_set(Set *set) { free(set->elements); free(set); } int main() { Element set1[] = {1, 2, 3}; Element set2[] = {2, 3, 4}; Set *s1 = create_set(set1, 3); Set *s2 = create_set(set2, 3); // 示例 Set *union_result = union_sets(s1, s2); // 输出并集 for (size_t i = 0; i < union_result->count; i++) { printf("%d ", union_result->elements[i]); } destroy_set(union_result); // 未实现集和对称,此处省略 return 0; } ``` 这个例子展示了如何通过自定义数据结构实现基础的集合操作,但请注意,这并不是标准C语言提供的功能,对于复杂场景可能需要借助第三方库或者使用更高级的语言特性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值