【數據結構】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("\n");
      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("\n");
      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("\n");
      return 1;

}
/*
* 輸出打標記后的各個元素
*輸入:打標記后的LA&LB
*/
int printfLinklist(Linklist *LA,Linklist *LB)
{
    Linklist *a,*b;
    a = LA;
    b = LB;
    printf("\nLA:");
    while(a->next!=NULL)
    {
        a = a->next;
        printf(" %d(%d)",a->data,a->flag);
    }
    printf("\nLB:");
    while(b->next!=NULL)
    {
        b = b->next;
        printf(" %d(%d)",b->data,b->flag);
    }
    printf("\n\n");
}




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;
}




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM