在线看毛片网站电影-亚洲国产欧美日韩精品一区二区三区,国产欧美乱夫不卡无乱码,国产精品欧美久久久天天影视,精品一区二区三区视频在线观看,亚洲国产精品人成乱码天天看,日韩久久久一区,91精品国产91免费

<abbr id="27omo"></abbr>

<menu id="27omo"><dl id="27omo"></dl></menu>
    • <label id="27omo"><tt id="27omo"></tt></label>

      新聞中心

      EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 集合交差并三種操作的C實(shí)現(xiàn)

      集合交差并三種操作的C實(shí)現(xiàn)

      作者: 時(shí)間:2016-12-01 來源:網(wǎng)絡(luò) 收藏

      /***************************************
      刪除集合中的某個(gè)元素

      參數(shù): 指向集合指針的指針
      需要?jiǎng)h除的元素
      返回值: 是否刪除成功
      ***************************************/
      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;
      }
      /*不存在當(dāng)前元素*/
      else if(head->next->value > element)
      break;
      else
      head = head->next;
      }
      return true;
      }

      /****************************************
      判斷元素是否在集合中
      參數(shù): 集合指針
      元素值
      返回值: 是否存在
      *****************************************/
      bool findElement(const Set *s, int Element)
      {
      // assert(s);
      if(s == NULL)
      {
      return false;
      }

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

      return false;
      }

      最重要的一個(gè)函數(shù),我認(rèn)為是集合的復(fù)制函數(shù),這有點(diǎn)類似在C++中的復(fù)制構(gòu)造或者賦值操作符函數(shù)。

      /****************************************
      實(shí)現(xiàn)集合的復(fù)制操作
      參數(shù): 一個(gè)指向被創(chuàng)建集合的指針
      一個(gè)集合指針
      返回: 判斷是否成功
      ****************************************/
      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;
      }

      最后是集合的三個(gè)操作:主要是利用了前面定義的一些函數(shù)實(shí)現(xiàn)的,所以說前面的問題處理好了,基本的操作就是手到擒來的事。

      /****************************************
      實(shí)現(xiàn)兩個(gè)集合的并集
      參數(shù):
      分別是兩個(gè)Set集合的指針
      返回值:
      一個(gè)集合的指針
      *****************************************/
      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;
      }

      /*******************************************
      實(shí)現(xiàn)兩個(gè)集合的交集
      參數(shù): 兩個(gè)集合指針
      返回值: 新的集合
      *******************************************/
      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;
      }



      評(píng)論


      技術(shù)專區(qū)

      關(guān)閉