微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 集合交差并三种操作的C实现

集合交差并三种操作的C实现

时间:12-01 来源:互联网 点击:

  1. head = (*s)-> head = (*s)->head;

    // assert(head);
    if(head == NULL)
    {
    return false;
    }
    /*表头更新*/
    if(head->value == element)
    {
    temp = head;
    if(head->next != NULL)
    head = head->next;
    else
    head = NULL;

    (*s)->head = head;
    (*s)->size --;
    free(temp);
    temp = NULL;

    return true;
    }

    while(head->next != NULL)
    {
    /*删除元素*/
    if(head->next->value == element)
    {
    temp = head->next;
    head->next = head->next->next;
    free(temp);
    (*s)->size --;
    temp = NULL;
    break;
    }
    /*不存在当前元素*/
    else if(head->next->value > element)
    break;
    else
    head = head->next;
    }
    return true;
    }

    /****************************************
    判断元素是否在集合中
    参数: 集合指针
    元素值
    返回值: 是否存在
    *****************************************/
    bool findElement(const Set *s, int Element)
    {
    // assert(s);
    if(s == NULL)
    {
    return false;
    }

    Node_t *head = s->head;
    while(head != NULL)
    {
    /*找得到当前值*/
    if(head->value == Element)
    return true;
    /*不存在当前值*/
    else if(head->value > Element)
    break;
    head = head->next;
    }

    return false;
    }

    最重要的一个函数,我认为是集合的复制函数,这有点类似在C++中的复制构造或者赋值操作符函数。

    /****************************************
    实现集合的复制操作
    参数: 一个指向被创建集合的指针
    一个集合指针
    返回: 判断是否成功
    ****************************************/
    bool copySet(Set **dst,const Set *src)
    {
    Node_t *head = NULL;

    assert(src);

    head = src->head;

    creat_nullset(dst);

    while(head != NULL)
    {
    if(!create_set(dst,head->value))
    return false;
    head = head->next;
    }

    return true;
    }

    最后是集合的三个操作:主要是利用了前面定义的一些函数实现的,所以说前面的问题处理好了,基本的操作就是手到擒来的事。

    /****************************************
    实现两个集合的并集
    参数:
    分别是两个Set集合的指针
    返回值:
    一个集合的指针
    *****************************************/
    Set * OrSets(const Set * s1, const Set *s2)
    {
    Set *news = NULL;
    const Set *searchset = NULL;
    Node_t *head = NULL;

    assert(s1 != NULL || s2 != NULL);

    if(get_setlength(s1) > get_setlength(s2))
    {
    copySet(&news,s1);
    searchset = s2;
    }
    else
    {
    copySet(&news, s2);
    searchset = s1;
    }
    head = searchset->head;
    while(head != NULL)
    {
    if(!create_set(&news, head->value))
    break;
    head = head->next;
    }
    return news;
    }

    /*******************************************
    实现两个集合的交集
    参数: 两个集合指针
    返回值: 新的集合
    *******************************************/
    Set * AndSets(const Set *s1, const Set *s2)
    {
    Set *newset = NULL;
    const Set *searchset = NULL;
    Node_t *head = NULL;

    assert(s1 != NULL && s2 != NULL);

    if(s1->head == NULL || s2->head == NULL)
    {
    /*空集合*/
    creat_nullset(&newset);
    return newset;
    }

    if(get_setlength(s1) > get_setlength(s2))
    {
    searchset = s1;
    head = s2->head;
    }
    else
    {
    searchset = s2;
    head = s1->head;
    }

    while(head != NULL)
    {
    if(findElement(searchset, head->value))
    {
    create_set(&newset, head->value);
    }
    head = head->next;
    }

    return newset;
    }

    /*********************************************
    集合的差集操作
    参数: 两个集合的指针
    返回值: 新的集合指针
    *********************************************/
    Set * XorSets(const Set *s1, const Set *s2)
    {
    Set *newset = NULL;
    Node_t *head = NULL;

    assert(s1 != NULL && s2 != NULL);

    if(s1->head == NULL)
    {
    /*空集合*/
    creat_nullset(&newset);
    return newset;
    }
    if(s2->head == NULL)
    {
    copySet(&newset,s1);
    return newset;
    }

    /*newset和s1是相同的*/
    copySet(&newset,s1);
    head = s1->head;

    while(head != NULL)
    {
    /*如果s2中存在当前元素,删除*/
    if(findElement(s2, head->value))
    {
    delete_setElement(&newset,head->value);
    }
    head = head->next;
    }

    return newset;
    }

    集合打印操作、集合的删除操作

    /*********************************************
    打印集合
    参数: 集合指针
    返回: void
    **********************************************/
    void print_set(const Set *s)
    {
    Node_t * head = NULL;
    int i = 0;
    assert(s);
    head = s->head;

    printf("{ ");

    while(head != NULL)
    {
    ++i;
    printf("%d ",head->value);

    if(i % 5 == 0 && i != 0)
    {
    i = 0;
    // printf("");
    }
    head = head->next;
    }
    printf("}");
    }

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top