求集合AUB

题目:

集合A:1 3 5 7 //顺序表A

集合B:5 7 9 11 //顺序表B

求AUB:1 3 5 7 9 11

代码:

 #include <stdio.h>
 #include <stdlib.h>
 #define N 8
 typedef struct seqlist
 {
    int data[N];
    int last;
 } seqlist_t, *seqlist_p;
 //创空
seqlist_p chuangkong()
 {
    seqlist_p p = (seqlist_p)malloc(sizeof(seqlist_t));
    if (NULL == p)
    {
        perror("malloc err");
        return NULL;
    }
    p->last = -1;
    return p;
 }
 //并集函数
void bingji(seqlist_p p, seqlist_p q, seqlist_p k)
 {
    int i = 0, j = 0, n = 0;
    //A,C中各拿出一个数比较大小,选出最小的
    //然后去跟C中数字比较是否相同,不同就存入C中
    while (i <= p->last && j <= q->last)
    {
        if (p->data[i] == q->data[j])
        {
            j++;
        }
        if (p->data[i] < q->data[j])
        {
            k->data[n++] = p->data[i++];
        }
        else
        {
            k->data[n++] = q->data[j++];
        }
    }
    while (i <= p->last)
    {
        k->data[n++] = p->data[i++];
    }
    while (j <= q->last)
    {
        k->data[n++] = q->data[j++];
    }
    k->last = n -1;
 }
//遍历
void bianli(seqlist_p p)
 {
    for (int i = 0; i <= p->last; i++)
        printf("%d ", p->data[i]);
    printf("\n");
 }
 int main(int argc, char const *argv[])
 {
    //集合A
    struct seqlist A = {{1, 3, 5, 7}, 3};
    seqlist_p p = &A;
    printf("顺序表A为:\n");
    bianli(p);
    //集合B
    struct seqlist B = {{5, 7, 9, 11}, 3};
    seqlist_p q = &B;
    printf("顺序表B为:\n");
    bianli(q);
    //集合C:AUB
    seqlist_p c = chuangkong();
    bingji(p, q, c);
    printf("顺序表C为:\n");
    bianli(c);
    return 0;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值