实验一的第二个又叫并集。

# include<stdio.h>
# include<stdlib.h>
# include<string.h>
# define MAX 1000
typedef struct List
{
    int ia[MAX];
    int Last;
} List;
List *LA; 
List *LB;
List *LC;
List *creat_list();
List *build_L(List*L);
void  Union_List(List *LA,List *LB);
void Print_List(List*L);
int  Length(List *L);
void insert_List(List*L,int x,int i);
int find_List(List*L,int ij);
int get_List(List*L, int i); 
void LC_List(List*LA);
int main() 
{  

   int ii=0;
   LA=creat_list();
   LB=creat_list();
   LC=creat_list(); 
   while(1)
         {
            system("cls");                                                    /*清屏函数*/
            fflush(stdin);                                                    /*清除键盘缓冲区*/
             puts("\t\t   欢迎进入数据系统");
          puts("===========================================================");     /*界面设计*/                          
          puts("|\t\t   请选择下列某一项 \t\t\t|");
          puts("|\t\t\t(1)创建A   \t\t\t|");
          puts("|\t\t\t(2)创建B   \t\t\t|");
          puts("|\t\t\t(3)合并A和B\t\t\t|");
          puts("|\t\t\t(4)显示数据\t\t\t|");   
          puts("|\t\t\t(5)退出    \t\t\t|"); 
          puts("===========================================================");   

          puts("请输入你的选择:");
          scanf("%d",&ii); 
              if(ii==5)
                    break;
              while(1)
                   {
                        if(ii<1||ii>5)
                        {                       
                           puts("输入错误,请重新输入\n");                           
                           scanf("%d",&ii);   
                        }
                        else
                           break;
                   }
              switch(ii)          
                    {
                        case 1:    build_L(LA); LC_List(LA);    getch();break;  
                        case 2:    build_L(LB);        getch();break;
                        case 3: Union_List(LA,LB);
                                puts("合并成功\n");                     
                                                       getch();break;
                        case 4: puts("A集合为:\n");    Print_List(LC);   printf("\n");
                                puts("B集合为:\n");   Print_List(LB);    printf("\n");
                                puts("并集合为:\n");  Print_List(LA);    printf("\n");
                                                                        getch();break;
                    } 
         }   
}
List *creat_list()
{
    List *L;
    L=(List*)malloc(sizeof(List));
             L->Last=-1;  

}
List *build_L(List*L)
{
  int i=0; 
  while(1)
  {     
    printf("(请输入数据(0结束))");
    scanf("%d",&L->ia[i]);  
    if(L->ia[i]==0)
       {  
         return (L);       
       }
    else
       {
        L->Last=L->Last+1; 
            i++;
       }    
  }         
}
void Print_List(List*L)
{
    int i;
    for(i=0;i<=L->Last;i++)
    {
        printf("ia[%2d]=%-2d",i+1,L->ia[i]);
        if((i+1)%5==0)
        {
            printf("\n");
        }
    }
}
int  Length(List *L)
{
    return(L->Last+1); 
}
int get_List(List *L, int i)
{   
    if(i<1||i>L->Last+1)
    {
        printf("非法位置!\n");
        return 0;
    }
    else
        return(L->ia[i-1]);
}
int find_List(List*L,int ij)
{
    int i;
    for(i=0;i<=L->Last;i++)
        if(L->ia[i]==ij)
        {
            return (i+1);
        } 
     return(0);    
}
void insert_List(List*L,int x,int i)
{
    int j;
    if(L->Last>=MAX-1)
    {   
        printf("数据溢出!\n");  
    }
    if(i<1||i>L->Last+2)
    {
        printf("没有这个位置!\n");
    }
    for(j=L->Last;j>=i-1;j--)
        L->ia[j+1]=L->ia[j];
    L->ia[i-1]=x;
    L->Last=L->Last+1;
}
void Union_List(List *LA,List *LB)
{
   int ii,ij,ik;
   int ix;  
   ij=Length(LA);   
   for(ii=1;ii<=LB->Last+1;ii++)    
       {
         ix=get_List(LB,ii);  
         ik=find_List(LA,ix);
         if(ik==0)
            {
                insert_List(LA,ix,ij+1);
                ij=Length(LA); 
            } 
       }    

}
void LC_List(List *LA)
{   
    int ii,ij;
    ij=Length(LA);
    for(ii=0;ii<ij;ii++)
        {
          LC->ia[ii]=LA->ia[ii]; 
          LC->Last=LC->Last+1;              
        }   
}

这个不知道用不用交,也一并附上。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值