jmu-ds-集合的并交差运算 (15 分)(单链表)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_42623428/article/details/85086028

有两个整数集合A和B,现在要求实现集合的并、交、差运算。例如A={2,7,9} ,B={3,7,12,2},则集合的并C=A∪B={2,7,9,3,12},而集合的交 C=A∩B={2,7},集合的差C=A-B={9}。集合A和B中元素个数在1~100之间。

输入格式:

三行,第一行分别为集合A,B的个数
第二行为A集合的数据
第三行为B集合的数据

输出格式:

三行
第一行集合并的结果:C的个数及C中的元素
第二行集合交的结果:C的个数及C中的元素
第三行集合差的结果:C的个数及C中的元素
输出结果以元素在A集合中的先后顺序输出,不能改变数据的输出顺序

输入样例:

3 4
2 7 9
3 7 12 2

输出样例:

5 2 7 9 3 12
2 2 7
1 9

 这道题目的后台数据有问题,下面的代码只能得10分。要想满分要改一点地方,输出c中元素个数之前要想判断是否小于等于5,是则输出,不是就不用输出,,,,很坑。

#include <bits/stdc++.h>
using namespace std;

typedef struct Node
{
    int data;
    struct Node* next;
}Node,*Linklist;

void Linklist_Create(Linklist &cl,int n)
{
    Linklist head;
    cl=(Linklist)malloc(sizeof(Node));
    cl->next=NULL;
    head=cl;
    for(int i=0;i<n;i++){
        Linklist t=(Linklist)malloc(sizeof(Node));
        int temp;
        cin>>temp;
        t->data=temp;
        head->next=t;
        head=head->next;
    }
    head->next=NULL;
}
void Print_Linklist(Linklist a)
{
    Linklist head=a->next;
    while(head!=NULL){
        if(head->next==NULL)cout<<head->data;
        else cout<<head->data<<" ";
        head=head->next;
    }
}
int Linklist_Union(Linklist a,Linklist b,Linklist &c){
    Linklist ha,hb,hc,t,temp;
    hb=b->next;
    ha=a->next;
    c=(Linklist)malloc(sizeof(Node));
    hc=c;
    int cnt=0;
    while(ha!=NULL){
        t=(Linklist)malloc(sizeof(Node));
        t->data=ha->data;
        hc->next=t;
        hc=hc->next;
        ha=ha->next;
        cnt++;
    }
    while(hb!=NULL){
        temp=hb;
        int f=0;
        ha=a->next;
        while(ha!=NULL){
            if(ha->data==temp->data){
                f=1;
                break;
            }
            else ha=ha->next;
        }
        if(!f){
            t=(Linklist)malloc(sizeof(Node));
            t->data=temp->data;
            hc->next=t;
            hc=hc->next;
            cnt++;
        }
        hb=hb->next;
    }
    hc->next=NULL;
    return cnt;
}
int Linklist_Intersect(Linklist a,Linklist b,Linklist &c){
    Linklist ha,hb,hc,t,temp;
    hb=b->next;
    ha=a->next;
    c=(Linklist)malloc(sizeof(Node));
    hc=c;
    int cnt=0;
    while(ha!=NULL){
        temp=ha;
        int f=0;
        hb=b->next;
        while(hb!=NULL){
            if(hb->data==temp->data){
                f=1;
                break;
            }
            else hb=hb->next;
        }
        if(f){
            t=(Linklist)malloc(sizeof(Node));
            t->data=temp->data;
            hc->next=t;
            hc=hc->next;
            cnt++;
        }
        ha=ha->next;
    }
    hc->next=NULL;
    return cnt;
}
int Linklist_Different(Linklist a,Linklist b,Linklist &c){
    Linklist ha,hb,hc,t,temp;
    hb=b->next;
    ha=a->next;
    c=(Linklist)malloc(sizeof(Node));
    hc=c;
    int cnt=0;
    while(ha!=NULL){
        temp=ha;
        int f=0;
        hb=b->next;
        while(hb!=NULL){
            if(hb->data==temp->data){
                f=1;
                break;
            }
            else hb=hb->next;
        }
        if(!f){
            t=(Linklist)malloc(sizeof(Node));
            t->data=temp->data;
            hc->next=t;
            hc=hc->next;
            cnt++;
        }
        ha=ha->next;
    }
    hc->next=NULL;
    return cnt;
}
int main()
{
    int n,m;
    cin>>n>>m;
    Linklist a,b,c1,c2,c3;
    Linklist_Create(a,n);
    Linklist_Create(b,m);
    int cntc1=Linklist_Union(a,b,c1);
    cout<<cntc1<<" ";
    Print_Linklist(c1);
    int cntc2=Linklist_Intersect(a,b,c2);
    cout<<endl;
    cout<<cntc2<<" ";
    Print_Linklist(c2);
    cout<<endl;
    int cntc3=Linklist_Different(a,b,c3);
    cout<<cntc3<<" ";
    Print_Linklist(c3);
}

猜你喜欢

转载自blog.csdn.net/qq_42623428/article/details/85086028