线性表的交集,并集,差集

#include
using namespace std;
const int Max=50;
    typedef struct
        {
            int a[Max];
            int length;
        }sqlist;
    void creat(sqlist &L,int n);
    void printlist(sqlist &L);
    void jiaoji(sqlist &L1,sqlist &L2,sqlist &L3); //交集
    void bingji(sqlist &L1,sqlist &L2,sqlist &L3);//并集
    void chaji(sqlist &L1,sqlist &L2,sqlist &L3);//差集

    int  main()
        {

            sqlist list1,list2,list3,list4,list5;
            cout<<"你想创建几个节点"<<endl;
            int n;
            cin>>n;
            creat(list1,n);
            printlist(list1);
            cout<<"你想创建几个节点"<<endl;
            cin>>n;
            creat(list2,n);
            printlist(list2);
            jiaoji(list1,list2,list3);
            printlist(list3);
            //************
            bingji(list1,list2,list4);
            printlist(list4);
            chaji(list1,list2,list5);
            printlist(list5);
            return 0;

        }

            void creat(sqlist &L,int n)
                {
                    int i=0;
                    cin>>L.a[i];
                    for(i=1;i<=n-1;i++)
                    {
                        cin>>L.a[i];

                    }

                    L.length=i;
                }


            void printlist(sqlist &L)
            {
                if(L.length==0)
                {
                    return ;
                }

                else
                {
                    int i;
                    for(i=0;i<=L.length-1;i++)
                    {
                        cout<<L.a[i]<<"  ";
                    }
                    cout<<endl;
                }
            }

            void jiaoji(sqlist &L1,sqlist &L2,sqlist &L3)//两个集合的交集
                {
                    int i=0,k=0,j;
                    while(i<=L1.length-1)
                    {
                        j=0;
                        while((j<=L2.length-1)&&(L2.a[j]!=L1.a[i]))
                        {
                            j++;
                        }
                        if(j!=L2.length)
                        {
                            L3.a[k]=L1.a[i];
                            k++;
                        }
                        i++;
                    }
                    L3.length=k;

                }

            void bingji(sqlist &L1,sqlist &L2,sqlist &L3) //两个集合的并集
                {
                    int i=0,j=0,k=0;
                    while(i<=L1.length-1)
                        {
                            L3.a[i]=L1.a[i];
                            i++;
                        }

                    k=0;
                    while(k<=L2.length-1)
                    {
                        j=0;
                        while(j<=L1.length-1&&L3.a[j]!=L2.a[k])
                        {
                            j++;
                        }

                        if(j==L1.length)
                        {
                            L3.a[i]=L2.a[k];
                            i++;
                        }
                        k++;
                    }

                    L3.length=i;
                }

                void chaji(sqlist &L1,sqlist &L2,sqlist &L3)  //求L1-L2的差集
                    {
                        int i=0,j=0,k=0;
                        while(i<=L1.length-1)
                        {
                            j=0;
                            while(j<=L2.length-1&&L2.a[j]!=L1.a[i])
                            {
                                j++;
                            }
                            if(j==L2.length)

                            {
                                L3.a[k]=L1.a[i];
                                k++;
                            }

                            i++;
                        }
                       L3.length=k;
                    }

你可能感兴趣的:(线性表的交集,并集,差集)