zoukankan      html  css  js  c++  java
  • 【数据结构】c语言实现集合的交并差运算

    待改写:存储数据类型int——>char
    重复的元素可存储
    功能上不完善

    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct
    {
        int data;
        int flag;
        struct Linklist *next;
    } Linklist;
    
    //对LA,LB中相同的元素打上标记
    Linklist* Link_get_flag(Linklist *LA,Linklist *LB)
    {
    
        Linklist *p = LA->next,*q = LB->next;
     //此处双重while循环目的是将LA,LB中相同的元素打上标记,便于求他们的交并差集。
        while(p != NULL)
        {
            while(q!=NULL)
            {
                if(p->data == q->data)
                {
                    p->flag = 1;
                    q->flag = 1;
                    q = q->next;
    
                }
                else
                {
                    q = q->next;
                }
    
            }
            q = LB->next;
            p = p->next;
    
        }
    
    
    
    }
    int creatLinklinst(Linklist *L,int i)
    {
        Linklist *p;
        L ->next = NULL;
    
        printf("依次输入元素各个值");
    
       for(;i>0;--i)
        {
            p  = (Linklist*)malloc(sizeof(Linklist));
            p->flag = 0;
            scanf("%d",&p->data);
            p->next = L->next;
            L->next = p;
        }
         return 1;
    
    }
    /*
    * 交集
    *输入:打标记后的LA/LB
    */
    int intersection(Linklist *L)
    {
        Linklist *node;
        Linklist *LC;
        LC = (Linklist *)malloc(sizeof(Linklist));
        LC->next = NULL;
    
        Linklist *p = L->next;
    
        while(p!=NULL)
    
    {
    
      //处理LC的数据域(*****)
    
              for(;p!=NULL;p = p->next)
          {
    
              if(p->flag == 1)
              {
                  node = (Linklist *)malloc(sizeof(Linklist));
                  node->data = p->data;
                  node->flag = 0;
                  node->next = LC->next ;
                  LC->next  = node;
              }
              else
              {
                  continue;
              }
    
          }
    
          printf("交集是:");
    
    
          while(LC->next!=NULL)
          {
              LC = LC->next;
              printf("%d ",LC->data);
    
          }
          printf("
    ");
          return 1;
    
    
    }
    
    
    }
    /*
    * 并集
    *输入:打标记后的LA&LB
    */
    int union_LALB(Linklist *LA,Linklist *LB)
    {
        Linklist *p = LA,*q = LB;
        Linklist *LC;
        LC = (Linklist *)malloc(sizeof(Linklist));
    
        LC->next = NULL;
    
        Linklist *node;
    
        while(p->next!=NULL)
        {
            if(p->flag==1)
            {
                p = p->next;
                continue;
            }
            else
            {
                p = p->next;
                node = (Linklist *)malloc(sizeof(Linklist));
                node->flag = 0;
                node->data = p->data;
    
                node->next = LC->next;
                LC->next = node;
    
    
            }
        }
         while(q->next!=NULL)
        {
                q = q->next;
                node = (Linklist *)malloc(sizeof(Linklist));
                node->flag = 0;
                node->data = q->data;
    
                node->next = LC->next;
                LC->next = node;
    
        }
          printf("并集是:");
    
    
          while(LC->next!=NULL)
          {
              LC = LC->next;
              printf("%d ",LC->data);
    
    
          }
          printf("
    ");
          return 1;
    
    
    }
    /*
    * 差集
    *输入:打标记后的LA&LB
    */
    int disset(Linklist *LA,Linklist *LB)
    {
        Linklist *p = LA,*q = LB;
        Linklist *LC;
        LC = (Linklist *)malloc(sizeof(Linklist));
    
        LC->next = NULL;
    
        Linklist *node;
    
        while(p->next!=NULL)
        {
            if(p->flag==1)
            {
                p = p->next;
                continue;
            }
            else
            {
                p = p->next;
                node = (Linklist *)malloc(sizeof(Linklist));
                node->flag = 0;
                node->data = p->data;
    
                node->next = LC->next;
                LC->next = node;
    
    
    
            }
        }
        printf("A-B的差集是:");
    
    
          while(LC->next!=NULL)
          {
              LC = LC->next;
              printf("%d ",LC->data);
    
          }
          printf("
    ");
          return 1;
    
    }
    /*
    * 输出打标记后的各个元素
    *输入:打标记后的LA&LB
    */
    int printfLinklist(Linklist *LA,Linklist *LB)
    {
        Linklist *a,*b;
        a = LA;
        b = LB;
        printf("
    LA:");
        while(a->next!=NULL)
        {
            a = a->next;
            printf(" %d(%d)",a->data,a->flag);
        }
        printf("
    LB:");
        while(b->next!=NULL)
        {
            b = b->next;
            printf(" %d(%d)",b->data,b->flag);
        }
        printf("
    
    ");
    }
    
    
    
    
    int main()
    {
        Linklist *LA,*LB;
        int i;
        LA = (Linklist *)malloc(sizeof(Linklist));
        LB = (Linklist *)malloc(sizeof(Linklist));
    //创建链表
        printf("输入LA长度:");
        scanf("%d",&i);
        creatLinklinst( LA,i);
    
        printf("输入LB长度:");
        scanf("%d",&i);
        creatLinklinst( LB,i);
    
    //打标记
        Link_get_flag(LA,LB);
        printfLinklist(LA,LB);
    
    //交集
        intersection(LA);
    //并集
        union_LALB(LA,LB);
    //差集
        disset(LA,LB);
    
    
    
        return 0;
    }
    
    
    
    
  • 相关阅读:
    2-3 vue配置介绍
    2-2 vue环境搭建以及vue-cli使用
    2-1 nodejs和npm的安装和环境搭建
    Babel
    vue-resource使用 (vue仿百度搜索)
    Google浏览器清除缓存快捷键
    browsersync即时刷新页面
    【高可用HA】Apache (2) —— Mac下安装多个Apache Tomcat实例
    【高可用HA】Apache (1) —— Mac下安装Apache Httpd到自定义路径(非/etc/apache2)
    Apache CXF实现Web Service(5)—— GZIP使用
  • 原文地址:https://www.cnblogs.com/zhujiaozhu/p/15376293.html
Copyright © 2011-2022 走看看