template <class Type> class Setlist i private. SetNode<type> first, last /序链表的表头指针,表尾指针 public Setlist o /构造函数 f first=last-=new SetNode<Type>(0);3 Setlist oi MakeEmpty( delete first; y void MakeEmpty(;∥空集合 int AddMember( const Type& x ) int DelMember( const Type&x)
template <class Type> class SetList { private: SetNode<Type> *first, *last; //有序链表的表头指针, 表尾指针 public: SetList ( ) //构造函数 { first = last = new SetNode<Type>(0); } ~SetList ( ) { MakeEmpty( ); delete first; } void MakeEmpty ( ); //置空集合 int AddMember ( const Type& x ); int DelMember ( const Type& x );
SetList<Type> operator=( Setlist<Type>& right);/将集合righ赋给集合this Setlist< Type> operator + Setlist<Type>& right )i /求集合this与集合 righti的并 Setlist<Type> operator*( Setlist<Type>& right /求集合this与集合 right的交 Setlist<Type> operator-( Setlist<Type>& right);)集合th与集合 right的差 int Contains( const Type &x ); /判x是否集合的成员 int operator =- Setlist<Type>& right ) /判集合this与集合 right相等
SetList<Type> & operator = ( SetList<Type>& right ); //将集合right赋给集合this SetList<Type> & operator + ( SetList<Type>& right ); //求集合this与集合right的并 SetList<Type> & operator * ( SetList<Type>& right ); //求集合this与集合right的交 SetList<Type> & operator - ( SetList<Type>& right ); //求集合this与集合right的差 int Contains ( const Type& x ); //判x是否集合的成员 int operator == ( SetList<Type>& right ); //判集合this与集合right相等
Iype&Min();/回集合中最小元素值 Type&Max();/返回集合中最大元素值 集合的搜索、加入和删除操作 template <class Type> int Setlist<Type>:: Contains( const Type&x)( 若x是集合成员,则函数返回1,否则返回0 SetNode<type>* temp= first->link; while( temp ! nUll &&e temp->data <x) temp=temp->link;/循链搜索
Type& Min ( ); //返回集合中最小元素值 Type& Max ( ); //返回集合中最大元素值 } 集合的搜索、加入和删除操作 template <class Type> int SetList<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 & temp->data --x) return 1 找到,返回1 else return O /未找到,返回0 template <class Type> int Setlist<Type>:: AddMember( const Type&x)( SetNode<Type>*p= first->link, *q=first; while(p!= null & p->data <x) q=p;p=p->link;}/循链扫描 if(p =nuLL & p->data)return 0 /集合中已有此元素
if ( temp != NULL && temp->data == x ) return 1; //找到, 返回1 else return 0; //未找到, 返回0 } template <class Type> int SetList<Type> :: AddMember ( const Type& x ) { SetNode<Type> *p = first->link, *q = first; while ( p != NULL && p->data < x ) { q = p; p = p->link; } //循链扫描 if ( p != NULL && p->data == x ) return 0; //集合中已有此元素
SetNode< type> xs= new SetNode(x); s->link=p;q-> link=s;/链入 if(p NULL last= s, /链到链尾时改链尾指针 return Ig template <class Type> int SetList<Type> DelMember( const Type&x)i SetNode<Type>* p first->link, *q- first while(p!=null & p->data x &q-p; p=p->link; 3 /循链扫描
SetNode<Type> * s = new SetNode (x); s->link = p; q->link = s; //链入 if ( p == NULL ) last = s; //链到链尾时改链尾指针 return 1; } template <class Type> int SetList<Type> :: DelMember ( const Type& x ) { SetNode<Type> * p = first->link, * q = first; while ( p != NULL && p->data < x ) { q = p; p = p->link; } //循链扫描