template <class Type> class Setlist i p rivate. SetNode<Type>ifirst, *last /)序链表的表头指针,表尾指针 public Setlist() /构造函数 i first=last=new SetNode<Type>(0);) aSetlist(( MakeEmpty; delete first; 3 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 );
小mg将集合rh给集合邮> Setlist<Type>& operator=( SetList< Type tList <Type> operator + Setlist<Type>& right);/求集合this与集合righ的并 Setlist<Type> operator * Setlist<Type>& right);/求集合this与集合righ的交 SetList<Type> operator-( SetList<Type>& right);/求集合this与集合righ的差 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相等
ype&Min(;/回集合中最小元素值 Iype&Max(;/回集合中最大元素值 集合的搜索、加入和删除操作 template <class Type> int Setlist<Type>:: Contains( const Type&x)( 若x是集合成员,则函数返回1,否则返回0 SetNode<type> x 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 0, 未找到,返回0 template <class Type> int Setlist<Type>:: AddMember( const Type&x)& SetNode<Type> *p=first-link,*q= first; while(p null & pr>data <x) {q=p;p= p->link;}循链扫描 if(p = null & p->data==x)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>*s- new SetNode(x); s-link=p;q-link=s;/链入 if(p=- NULL last=s, 链到链尾时改链尾指针 return I template <class Type> int Setlist<Type>: DelMember( const Type&x SetNode<Type>* p= first->ink,*q=first while(p! =nULL & p->data <x) {q=p;p=p-link;}/循链扫描
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; } //循链扫描