正在加载图片...
第7章集合与搜索 A+B=(A-B)+(B-A)+A'B 【解答】 若设集合A={1,3,4,7,9,15},集合B={2,3,5,6,7,12,15,17}。则 A+B={1,2,3,4,5,6,7,9,12,15,17} 又A*B={3,7,15},A-B={1,4,9},B-A={2,5,6,12,17} 则 (A-B)+(B-A)+A*B={1,2,3,4,5,6,7,9,12,15,17} 有A+B=(A-B)+(B-A)+A*B。 7-7给定一个用无序链表表示的集合,需要在其上执行 operator+() operator'() operator-() Contains(x), EndMember(x), Delmemben(x),Mn(),试写出它的类声明,并给出所有这些成员函数的实现 【解答】 下面给出用无序链表表示集合时的类的声明。 template <class Type> class Set 用以表示集合的无序链表的类的前视定义 template <class Type> class SetNode i ∥集合的结点类定义 friend class Setlist<type> private ∥每个成员的数据 MetHode<Iype>·lnk; 指针 Methode( const Type&iem):da(ilem),lnk(NULL;∥构造函数 template <class Type> class Set ∥集合的类定义 SetNode <Type> * first, "last: 无序链表的表头指针,表尾指针 Setlist()first=last= new SetNode <Type>(0);) 构造函数 -Setlist (i MakeEmpry ( delete first;1 ∥析构函数 oid Make Empty ( 置空集合 int AddMember( const Type& x )i 把新元素x加入到集合之中 int DelMember( const Type& x ) 把集合中成员x删去 Set <Type>& operator = Set <Type>& right ) ∥复制集合rght到 Set <Type>& operator + Set <Type>& right ) ∥集合this与集合rght的并 Set <Type>& operator ' Set <Type>& right ) 集合this与集合 right I的交 Set <Type>& operator-( Set <Type>& right ) ∥集合this与集合rght的差 int Contains( const Type& x ) 判x是否集合的成员 int operator == Set <Type>& right ) 判集合this与集合 right相等 Type& Min (; 返回集合中的最小元素的值 (I)operator+o template <class Type> Set <Type>& Set <Type>: operator + Set <Type>& right )t ∥求集合this与集合 right的并,计算结果通过临时集合lemp返回,this集合与rght集合不变。第 7 章 集合与搜索 56 A + B = (A - B) + (B - A) + A * B 【解答】 若设集合 A= { 1, 3, 4, 7, 9, 15 },集合 B = { 2, 3, 5, 6, 7, 12, 15, 17 }。则 A + B = { 1, 2, 3, 4, 5, 6, 7, 9, 12, 15, 17 } 又 A * B = { 3, 7, 15 }, A – B = { 1, 4, 9 }, B – A = { 2, 5, 6, 12, 17 } 则 (A – B) + (B – A) + A * B = { 1, 2, 3, 4, 5, 6, 7, 9, 12, 15, 17 } 有 A + B = (A – B) + ( B – A ) + A * B。 7-7 给定一个用无序链表表示的集合,需要在其上执行 operator+( ), operator*( ), operator- ( ), Contains(x), AddMember (x), DelMember(x), Min( ),试写出它的类声明,并给出所有这些成员函数的实现。 【解答】 下面给出用无序链表表示集合时的类的声明。 template <class Type> class Set; //用以表示集合的无序链表的类的前视定义 template <class Type> class SetNode { //集合的结点类定义 friend class SetList<Type>; private: Type data; //每个成员的数据 SetNode <Type> *link; //链接指针 public: SetNode (const Type& item ) : data (item), link (NULL); //构造函数 }; template <class Type> class Set { //集合的类定义 private: SetNode <Type> *first, *last; //无序链表的表头指针, 表尾指针 public: SetList ( ) {first = last = new SetNode <Type>(0); } //构造函数 ~SetList ( ) { MakeEmpty ( ); delete first; } //析构函数 void MakeEmpty ( ); //置空集合 int AddMember ( const Type& x ); //把新元素 x 加入到集合之中 int DelMember ( const Type& x ); //把集合中成员 x 删去 Set <Type>& operator = ( Set <Type>& right ); //复制集合 right 到 this。 Set <Type>& operator + ( Set <Type>& right ); //求集合 this 与集合 right 的并 Set <Type>& operator * ( Set <Type>& right ); //求集合 this 与集合 right 的交 Set <Type>& operator - ( Set <Type>& right ); //求集合 this 与集合 right 的差 int Contains ( const Type& x ); //判 x 是否集合的成员 int operator == ( Set <Type>& right ); //判集合 this 与集合 right 相等 Type& Min ( ); //返回集合中的最小元素的值 } (1) operator + ( ) template <class Type> Set <Type>& Set <Type> :: operator + ( Set <Type>& right ) { //求集合 this 与集合 right 的并, 计算结果通过临时集合 temp 返回,this 集合与 right 集合不变
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有