正在加载图片...
第7章集合与搜索 while(Pa I= NULL) ∥将集合this中元素一个个拿出到 right中查重 /right集合的链扫描指针 while( pb l= NULL & pa->data I= pb->data if(pb==NULL) 批此this中的元素在 right中未找到,插入 ipc->link new SetNode <Type>( pa->data ) pc= pc->link; pa=pa->ink pe>link=NULL; last=pc ∥链表收尾 return lemp (4)Contains(x) template <class Type> int Set <Type>:: Contains( const Type& x) 测试函数:如果x是集合的成员,则函数返回1,否则返回0。 SetNode<Type>*temp=first-> ink ∥链的扫描指针 while( temp=NULL&&lemp->daal=x)lemp=lemp->lmk;∥循链搜索 if( temp I=NULL)return 1; ∥找到,返回1 Ise return 0; ∥未找到,返回 (5)AddMember(x) template <class Type> int Set <Type>: AddMeml st Type& x)i ∥把新元素x加入到集合之中。若集合中已有此元素,则函数返回0,否则函数返回1 SetNode<Type>*temp =first-> ink; ∥temp是扫描指针 while( temp I=NULL & temp->data I=x )temp= temp-> ink; /循链扫描 if( temp I= NULL)return 0; ∥集合中己有此元素,不加 last= last->link new SetNode(x); ∥否则,创建数据值为x的新结点,链入 return 1: (6) DelMember(x) template <class Type> int Set <Type>: DelMember( const Type&x)i ∥把集合中成员x删去。若集合不空且元素x在集合中,则函数返回1,否则返回0。 ode<Type>*p=first-> ink, *q while(P= NULL)i ∥找到 ∥重新链接 if (p==last)last=q ∥删去链尾结点时改链尾指针 delete p: return ∥删除含x结点 else( g=p; p=p->link;) ∥循链扫描 return 0; ∥集合中无此元素 template <class Type> SetNode<Type>" Set <Type>: Min (t第 7 章 集合与搜索 58 while ( pa != NULL ) { //将集合 this 中元素一个个拿出到 right 中查重 SetNode <Type> *pb = right.first->link; //right 集合的链扫描指针 while ( pb != NULL && pa->data != pb->data ) pb = pb->link; if ( pb == NULL ) //此 this 中的元素在 right 中未找到, 插入 { pc->link = new SetNode <Type> ( pa->data ); pc = pc->link; } pa = pa->link; } pc->link = NULL; last = pc; //链表收尾 return temp; } (4) Contains(x) template <class Type> int Set <Type> :: Contains ( const Type& x ) { //测试函数: 如果 x 是集合的成员, 则函数返回 1, 否则返回 0。 SetNode<Type> * temp = first->link; //链的扫描指针 while ( temp != NULL && temp->data != x ) temp = temp->link; //循链搜索 if ( temp != NULL ) return 1; //找到, 返回 1 else return 0; //未找到, 返回 0 } (5) AddMember (x) template <class Type> int Set <Type> :: AddMember ( const Type& x ) { //把新元素 x 加入到集合之中。若集合中已有此元素, 则函数返回 0, 否则函数返回 1。 SetNode<Type> * temp = first->link; // temp 是扫描指针 while ( temp != NULL && temp->data != x ) temp = temp->link; /循链扫描 if ( temp != NULL ) return 0; //集合中已有此元素, 不加 last = last->link = new SetNode (x); //否则, 创建数据值为 x 的新结点, 链入 return 1; } (6) DelMember (x) template <class Type> int Set <Type> :: DelMember ( const Type& x ) { //把集合中成员 x 删去。若集合不空且元素 x 在集合中, 则函数返回 1, 否则返回 0。 SetNode<Type> * p = first->link, *q = first; while ( p != NULL ) { if ( p->data == x ) { //找到 q->link = p->link; //重新链接 if ( p == last ) last = q; //删去链尾结点时改链尾指针 delete p; return 1; //删除含 x 结点 } else { q = p; p = p->link; } //循链扫描 return 0; //集合中无此元素 } (7) Min ( ) template <class Type> SetNode<Type> * Set <Type> :: Min ( ) {
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有