新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 集合交差并三種操作的C實現

        集合交差并三種操作的C實現

        作者: 時間:2016-12-01 來源:網絡 收藏

        /***************************************
        刪除集合中的某個元素

        參數: 指向集合指針的指針
        需要刪除的元素
        返回值: 是否刪除成功
        ***************************************/
        bool delete_setElement(Set **s, int element)
        {
        Node_t *head = NULL;
        Node_t *temp = NULL;

        assert(*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;
        }



        評論


        技術專區

        關閉
        主站蜘蛛池模板: 满洲里市| 靖江市| 湛江市| 开远市| 上高县| 靖州| 密山市| 鄱阳县| 平凉市| 建平县| 宁城县| 宜阳县| 吉首市| 黔西县| 黄平县| 区。| 北碚区| 山阳县| 建水县| 静宁县| 南部县| 莲花县| 衡水市| 泽州县| 凤山县| 大埔县| 原阳县| 平利县| 德化县| 定西市| 浦城县| 于田县| 淮安市| 丰都县| 沧源| 威宁| 宣化县| 锡林浩特市| 滨海县| 五河县| 会昌县|