Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1861461
  • 博文数量: 152
  • 博客积分: 3730
  • 博客等级: 上尉
  • 技术积分: 3710
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-02 14:36
个人简介

减肥,运动,学习,进步...

文章分类

全部博文(152)

文章存档

2016年(14)

2015年(17)

2014年(16)

2013年(4)

2012年(66)

2011年(35)

分类: C/C++

2012-08-25 11:47:40

    这段时间一直在看C++相关的数据结构,感觉STL库的出现确实给C++实现一些基本的数据结构更加的方便,特别是map、list、set、vector的灵活运用能实现很多强大的数据结构,记得在一本书中曾经读到过“C++中算法的重要性没有C语言中那么明显,而是设计方法的问题”这句话的正确性有待于进一步的考察,但是面向对象中的更多的是依靠继承多态性实现多对象编程,而在C语言中更多的事依靠算法和数据结构。花了一晚上采用C语言实现了一个简单的集合操作,支持集合的创建,元素的查询,删除,支持集合的交集、差集、并集计算。
 
    首先说明一下集合的ADT,肯定有包括基本的创建、删除操作。以及某一个元素的插入和删除操作。集合的一个重要特征就是元素的不重复性。这种方式我在实现的过程中通过链表管理一个集合,按照元素的大小进行排列,之所以排列主要是为了方便删除、查找元素的操作。实质上集合对元素的顺序是没有要求的。
    集合的交集:是指两个集合元素相同的部分构成的集合。
    集合的差集:是指其中一个集合中除去另一个集合相同元素以后剩余的元素构成的集合。
    集合的并集:是指两个集合的所有元素构成的集合。
 
    其中需要注意的就是集合的元素是不能重复的,本来我打算采用二叉查找树的方式实现,因为这种树型结构便于快速的查询,但是我采用元素的升序排序就能避免集合中漫无目的的查询操作。毕竟实现二叉查找树也增加了一个指针成员,而且还需要大量的操作。
 
简要的说明一下我的过程:
基本的集合结构体:

点击(此处)折叠或打开

  1. typedef struct linknode
  2. {
  3.     struct linknode *next;
  4.     int value;
  5. }Node_t, *Node_handle_t;

  6. typedef struct set
  7. {
  8.     unsigned int size;
  9.     Node_handle_t head;
  10. }Set;
 
集合的创建,该函数的实现是集合实现的最复杂的一个函数,之所以这么复杂是因为很多的操作都是基于该函数完成的,当然也可以采用更精细的函数来实现,我开始的想法是将元素插入和创建操作合并,都基于一个函数进行操作,但是写完以后发现函数太长,而且比较烂,不够必将还是完成了一些基本的操作。采用了升序存储的方式,主要是为了后续的查找操作,注意链表的更新操作。

点击(此处)折叠或打开

  1. bool create_set(Set **sets, int data)
  2. {
  3.     Set * temp = (Set *)malloc(sizeof(Set)/sizeof(char));
  4.     Node_t *node = NULL;
  5.     Node_t *head = NULL;

  6.     if(*sets == NULL)
  7.     {
  8.         temp->size = 0;
  9.         temp->head = NULL;
  10.         *sets = temp;
  11.         node = (Node_t *)malloc(sizeof(Node_t)/sizeof(char));
  12.         if(node != NULL)
  13.         {
  14.             node->next = NULL;
  15.             node->value = data;
  16.             /*更新集合指针*/
  17.             temp->head = node;
  18.             temp->size ++;
  19.             *sets = temp;
  20.             
  21.             temp = NULL;
  22.             return true;
  23.         }
  24.     }
  25.     else/*已经存在部分集合*/
  26.     {
  27.         /******************************
  28.         采用顺序存储的方式插入新的元素
  29.         首先查找是否存在值,有不做任何操作
  30.         不存在,则按值大小找到合适的位置
  31.         ******************************/

  32.         /*遍历*/
  33.         if((*sets)->head != NULL)
  34.         {
  35.             /*更新表头*/
  36.             if((*sets)->head->value > data)
  37.             {
  38.                 node = (Node_t *)malloc(sizeof(Node_t)/sizeof(char));
  39.                 if(node == NULL)
  40.                     return false;
  41.                 node->next = (*sets)->head;
  42.                 node->value = data;
  43.                 (*sets)->head = node;
  44.                 (*sets)->size ++;

  45.                 return true;
  46.             }
  47.             else if((*sets)->head->value == data)
  48.             {
  49.                 return true;
  50.             }

  51.             /*从下一个节点开始*/
  52.             head = (*sets)->head;
  53.             node = head;

  54.             while(head->next != NULL)
  55.             {
  56.                 /*已经存在*/
  57.                 if(head->next->value == data)
  58.                 {
  59.                     return true;
  60.                 }
  61.                 /*找到合适的位置,并插入*/
  62.                 else if(head->next->value > data)
  63.                 {
  64.                     node = (Node_t *)malloc(sizeof(Node_t)/sizeof(char));
  65.                     if(node == NULL)
  66.                         return false;
  67.                     node->value = data;
  68.                     node->next = head->next;
  69.                     head->next = node;
  70.                     (*sets)->size ++;

  71.                     return true;
  72.                 }
  73.                 else
  74.                 {
  75.                     head = head->next;
  76.                 }
  77.             }
  78.             /*说明以上不存在该值*/
  79.             node = (Node_t *)malloc(sizeof(Node_t)/sizeof(char));
  80.             if(node == NULL)
  81.                 return false;
  82.             node->value = data;
  83.             node->next = NULL;
  84.             head->next = node;
  85.             (*sets)->size ++;
  86.             return true;
  87.         }
  88.         else
  89.         {
  90.             node = (Node_t *)malloc(sizeof(Node_t)/sizeof(char));
  91.             if(node == NULL)
  92.                 return false;
  93.             node->value = data;
  94.             node->next = NULL;
  95.             (*sets)->head = node;
  96.             (*sets)->size ++;
  97.             return true;
  98.         }
  99.     }
  100.     return false;
  101. }

查找、删除元素操作,充分利用了前面的升序存储关系。

点击(此处)折叠或打开

  1. /***************************************
  2.         删除集合中的某个元素

  3. 参数:        指向集合指针的指针
  4.             需要删除的元素
  5. 返回值:    是否删除成功
  6. ***************************************/
  7. bool delete_setElement(Set **s, int element)
  8. {
  9.     Node_t *head = NULL;
  10.     Node_t *temp = NULL;
  11.     
  12.     assert(*s);

  13.     head = (*s)->head;
  14.     
  15. //    assert(head);
  16.     if(head == NULL)
  17.     {
  18.         return false;
  19.     }
  20.     /*表头更新*/
  21.     if(head->value == element)
  22.     {
  23.         temp = head;
  24.         if(head->next != NULL)
  25.             head = head->next;
  26.         else
  27.             head = NULL;

  28.         (*s)->head = head;
  29.         (*s)->size --;
  30.         free(temp);
  31.         temp = NULL;

  32.         return true;
  33.     }

  34.     while(head->next != NULL)
  35.     {
  36.         /*删除元素*/
  37.         if(head->next->value == element)
  38.         {
  39.             temp = head->next;
  40.             head->next = head->next->next;
  41.             free(temp);
  42.             (*s)->size --;
  43.             temp = NULL;
  44.             break;
  45.         }
  46.         /*不存在当前元素*/
  47.         else if(head->next->value > element)
  48.             break;
  49.         else
  50.             head = head->next;
  51.     }
  52.     return true;
  53. }

  54. /****************************************
  55.             判断元素是否在集合中
  56. 参数:        集合指针
  57.             元素值
  58. 返回值:        是否存在
  59. *****************************************/
  60. bool findElement(const Set *s, int Element)
  61. {
  62. //    assert(s);
  63.     if(s == NULL)
  64.     {
  65.         return false;
  66.     }

  67.     Node_t *head = s->head;
  68.     while(head != NULL)
  69.     {
  70.         /*找得到当前值*/
  71.         if(head->value == Element)
  72.             return true;
  73.         /*不存在当前值*/
  74.         else if(head->value > Element)
  75.             break;
  76.         head = head->next;
  77.     }

  78.     return false;
  79. }

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

点击(此处)折叠或打开

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

  10.     assert(src);
  11.     
  12.     head = src->head;

  13.     creat_nullset(dst);
  14.     
  15.     while(head != NULL)
  16.     {
  17.         if(!create_set(dst,head->value))
  18.             return false;
  19.         head = head->next;
  20.     }

  21.     return true;
  22. }

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

点击(此处)折叠或打开

  1. /****************************************
  2.         实现两个集合的并集
  3. 参数:
  4.         分别是两个Set集合的指针
  5. 返回值:
  6.         一个集合的指针
  7. *****************************************/
  8. Set * OrSets(const Set * s1, const Set *s2)
  9. {
  10.     Set *news = NULL;
  11.     const Set *searchset = NULL;
  12.     Node_t *head = NULL;

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

  14.     if(get_setlength(s1) > get_setlength(s2))
  15.     {
  16.         copySet(&news,s1);
  17.         searchset = s2;
  18.     }
  19.     else
  20.     {
  21.         copySet(&news, s2);
  22.         searchset = s1;
  23.     }
  24.     head = searchset->head;
  25.     while(head != NULL)
  26.     {
  27.         if(!create_set(&news, head->value))
  28.             break;
  29.         head = head->next;
  30.     }
  31.     return news;
  32. }

  33. /*******************************************
  34.             实现两个集合的交集
  35. 参数:        两个集合指针
  36. 返回值:    新的集合
  37. *******************************************/
  38. Set * AndSets(const Set *s1, const Set *s2)
  39. {
  40.     Set *newset = NULL;
  41.     const Set *searchset = NULL;
  42.     Node_t *head = NULL;
  43.     
  44.     assert(s1 != NULL && s2 != NULL);

  45.     if(s1->head == NULL || s2->head == NULL)
  46.     {
  47.             /*空集合*/
  48.             creat_nullset(&newset);
  49.             return newset;
  50.     }

  51.     if(get_setlength(s1) > get_setlength(s2))
  52.     {
  53.         searchset = s1;
  54.         head = s2->head;
  55.     }
  56.     else
  57.     {
  58.         searchset = s2;
  59.         head = s1->head;
  60.     }
  61.     
  62.     while(head != NULL)
  63.     {
  64.         if(findElement(searchset, head->value))
  65.         {
  66.             create_set(&newset, head->value);
  67.         }
  68.         head = head->next;
  69.     }
  70.     
  71.     return newset;
  72. }

  73. /*********************************************
  74.             集合的差集操作
  75. 参数:        两个集合的指针
  76. 返回值:        新的集合指针
  77. *********************************************/
  78. Set * XorSets(const Set *s1, const Set *s2)
  79. {
  80.     Set *newset = NULL;
  81.     Node_t *head = NULL;
  82.     
  83.     assert(s1 != NULL && s2 != NULL);

  84.     if(s1->head == NULL)
  85.     {
  86.             /*空集合*/
  87.             creat_nullset(&newset);
  88.             return newset;
  89.     }
  90.     if(s2->head == NULL)
  91.     {
  92.         copySet(&newset,s1);
  93.         return newset;
  94.     }

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

  98.     while(head != NULL)
  99.     {
  100.         /*如果s2中存在当前元素,删除*/
  101.         if(findElement(s2, head->value))
  102.         {
  103.             delete_setElement(&newset,head->value);
  104.         }
  105.         head = head->next;
  106.     }
  107.     
  108.     return newset;
  109. }

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

点击(此处)折叠或打开

  1. /*********************************************
  2.             打印集合
  3. 参数:        集合指针
  4. 返回:        void
  5. **********************************************/
  6. void print_set(const Set *s)
  7. {
  8.     Node_t * head = NULL;
  9.     int i = 0;
  10.     assert(s);
  11.     head = s->head;

  12.     printf("{ ");

  13.     while(head != NULL)
  14.     {
  15.         ++i;
  16.         printf("%d ",head->value);

  17.         if(i % 5 == 0 && i != 0)
  18.         {
  19.             i = 0;
  20.         //    printf("\n");
  21.         }
  22.         head = head->next;
  23.     }
  24.     printf("}\n");
  25. }

  26. /*********************************************
  27.             删除一个集合
  28. 参数:        指向集合指针的指针
  29. 返回值:    void
  30. **********************************************/
  31. void delete_set(Set **s)
  32. {
  33.     Node_t *head = NULL;
  34.     Node_t *temp = NULL;

  35. //    assert(*s);
  36.     if(*s == NULL)
  37.         return;

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

  39.     while(head != NULL)
  40.     {
  41.         temp = head;
  42.         head = head->next;
  43.         free(temp);
  44.         (*s)->size --;

  45.         temp = NULL;
  46.     }
  47.     free(*s);
  48.     *s = NULL;
  49. }

最后是我的测试程序:

点击(此处)折叠或打开

  1. /************************************************
  2.             主函数
  3.         完成各个函数的测试工作
  4.         最后需要完成内存的释放
  5. ************************************************/
  6. int main()
  7. {
  8.     Set* sets1 = NULL;
  9.     Set* sets2 = NULL;
  10.     Set* sets3 = NULL;
  11.     Set* sets4 = NULL;

  12.     int i = 0;
  13.     int j = 0;
  14.     for(i = 0; i < 10; ++ i)
  15.     {
  16.         if(!create_set(&sets1, i+10))
  17.             break;
  18.         j = i + 10 - 5;

  19.         if(!create_set(&sets2, j))
  20.             break;
  21.     }
  22.     printf("Set1 : ");
  23.     print_set(sets1);
  24.     printf("Set2 : ");
  25.     print_set(sets2);

  26.     sets3 = OrSets(sets1,sets2);
  27.     printf("Set1 + Set2: ");
  28.     print_set(sets3);

  29.     sets3 = OrSets(sets2,sets1);
  30.     printf("Set2 + Set1: ");
  31.     print_set(sets3);

  32.     delete_set(&sets3);
  33.     sets3 = AndSets(sets1,sets2);
  34.     printf("Set1 * Set2: ");
  35.     print_set(sets3);

  36.     delete_set(&sets3);
  37.     sets3 = AndSets(sets2,sets1);
  38.     printf("Set2 * Set1: ");
  39.     print_set(sets3);

  40.     delete_set(&sets3);
  41.     sets3 = XorSets(sets1,sets2);
  42.     printf("Set1 - Set2: ");
  43.     print_set(sets3);

  44.     delete_set(&sets3);
  45. //    creat_nullset(&sets4);
  46.     sets3 = XorSets(sets2,sets1);
  47.     printf("Set2 - Set1: ");
  48.     print_set(sets3);

  49.     delete_set(&sets1);
  50.     delete_set(&sets2);
  51.     delete_set(&sets3);
  52.     delete_set(&sets4);
  53.     return 0;
  54. }

实验效果:

总结:

该实现并没有考虑性能方面,基本上实现了集合的简单操作。后面有时间再想想如何优化查找和删除操作。





阅读(6451) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~