集合的运算

#define len sizeof(struct order)
#include<string.h>
struct order
{char num;
 struct order *next;
 };
void main()
{int i,j;
char p1[30],p2[30];
struct order *b,*bpre,*bhead,*a,*apre,*ahead,*p;
 printf("/nenter the first chars :");
    gets(p1);
order(p1,strlen(p1));
 printf("/nenter the second chars :");
    gets(p2);
order(p2,strlen(p2));
a=apre=(struct order*)malloc(len);
 for(i=0;i<strlen(p1);i++)
   if((p1[i]>='a')&&(p1[i]<='z'))
     break;
a->num=p1[i];
ahead=a;
for(j=i+1;j<=strlen(p1);j++)
  { a=(struct order*)malloc(len);
    while(p1[j-1]==p1[j])
       j=j+1;
    a->num=p1[j];
    apre->next=a;
    apre=a;
 }
a->next=0;
a=ahead;
b=bpre=(struct order*)malloc(len);
for(i=0;i<strlen(p2);i++)
     if((p2[i]>='a')&&(p2[i]<='z'))
 break;
b->num=p2[i];
bhead=b;
for(j=i+1;j<=strlen(p2);j++)
   { b=(struct order*)malloc(len);
       while(p2[j-1]==p2[j])
  j=j+1;
     b->num=p2[j];
     bpre->next=b;
     bpre=b;
   }
b->next=0;
b=bhead;
printf("the p1Up2 is:");
while(a->next!=0&&b->next!=0)         //判断并集  注意指针的移动
{if(a->num==b->num)
   {  printf("%c",a->num);
      a=a->next;
      b=b->next;}
 if(a->num<b->num)
   {  printf("%c",a->num);
      a=a->next;}
 if(a->num>b->num)
   {  printf("%c",b->num);
      b=b->next;}
 }
while(a->next!=0)                  //是否还有a,b剩余的情况
  {  printf("%c",a->num);
     a=a->next;}
while(b->next!=0)
  {  printf("%c",b->num);
     b=b->next;}
a=ahead;b=bhead;
printf("/nthe p1%cp2 is:",239);      //交集
while(a->next!=0&&b->next!=0)
  { if(a->num==b->num)
     { printf("%c",a->num);
       a=a->next;
       b=b->next;
     }
    if(a->num<b->num)
       a=a->next;
    if(a->num>b->num)
       b=b->next;
}
a=ahead;b=bhead;
printf("/nthe p1-p2 is:");
while(a->next!=0&&b->next!=0)      //注意a指针的移动
{   if(a->num==b->num)
      { p=a;
 a=a->next;
 b=b->next;
       if(p==ahead)
   ahead=a;
       else
  apre->next=a;
 free(p);
      }
    if(a->num<b->num)
      { apre=a;
 a=a->next;
      }
    if(a->num>b->num)
 b=b->next;
  }
a=ahead;
while(a->next!=0)
{ printf("%c",a->num);
  a=a->next;
 }
a=ahead;b=bhead;
getch();
}
order(char a[15],int n)      //为字符串排序
{ int i,j,k;
  char t;
 for(i=0;i<n-1;i++)
   { k=i;
     for(j=i+1;j<n;j++)
       if(a[j]<=a[k]) k=j;
  t=a[k];a[k]=a[i];a[i]=t;
   }
}

总结:将字符串排序,相同的去掉,然后入链表
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值