集合交差并三种操作的C实现
head = (*s)-> head = (*s)->head; (*s)->head = head; return true; while(head->next != NULL) /**************************************** Node_t *head = s->head; return false; 最重要的一个函数,我认为是集合的复制函数,这有点类似在C++中的复制构造或者赋值操作符函数。 /**************************************** assert(src); creat_nullset(dst); return true; 最后是集合的三个操作:主要是利用了前面定义的一些函数实现的,所以说前面的问题处理好了,基本的操作就是手到擒来的事。 /**************************************** assert(s1 != NULL || s2 != NULL); if(get_setlength(s1) > get_setlength(s2)) /******************************************* if(s1->head == NULL || s2->head == NULL) if(get_setlength(s1) > get_setlength(s2)) /********************************************* if(s1->head == NULL) /*newset和s1是相同的*/ while(head != NULL) 集合打印操作、集合的删除操作 /********************************************* printf("{ "); while(head != NULL) if(i % 5 == 0 && i != 0)
// assert(head);
if(head == NULL)
{
return false;
}
/*表头更新*/
if(head->value == element)
{
temp = head;
if(head->next != NULL)
head = head->next;
else
head = NULL;
(*s)->size --;
free(temp);
temp = 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;
}
while(head != NULL)
{
/*找得到当前值*/
if(head->value == Element)
return true;
/*不存在当前值*/
else if(head->value > Element)
break;
head = head->next;
}
}
实现集合的复制操作
参数: 一个指向被创建集合的指针
一个集合指针
返回: 判断是否成功
****************************************/
bool copySet(Set **dst,const Set *src)
{
Node_t *head = NULL;
head = src->head;
while(head != NULL)
{
if(!create_set(dst,head->value))
return false;
head = head->next;
}
}
实现两个集合的并集
参数:
分别是两个Set集合的指针
返回值:
一个集合的指针
*****************************************/
Set * OrSets(const Set * s1, const Set *s2)
{
Set *news = NULL;
const Set *searchset = NULL;
Node_t *head = NULL;
{
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);
{
/*空集合*/
creat_nullset(&newset);
return newset;
}
{
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);
{
/*空集合*/
creat_nullset(&newset);
return newset;
}
if(s2->head == NULL)
{
copySet(&newset,s1);
return newset;
}
copySet(&newset,s1);
head = s1->head;
{
/*如果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;
{
++i;
printf("%d ",head->value);
{
i = 0;
// printf("");
}
head = head->next;
}
printf("}");
}
集合交差三种操作C实 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)