下载此文档

c++集合类.doc


文档分类:办公文档 | 页数:约13页 举报非法文档有奖
1/13
下载提示
  • 1.该资料是网友上传的,本站提供全文预览,预览什么样,下载就什么样。
  • 2.下载该文档所得收入归上传者、原创者。
  • 3.下载的文档,不会出现我们的网址水印。
1/13 下载此文档
文档列表 文档介绍
题目:编写一个集合类,实现集合的相应运算。截图:源代码:#include<iostream>usingnamespacestd;typedefstructnode{intdata;structnode*next;}Node,*ptrNode,*list;classSet{private:listL;//采用带头结点的链表存储集合元素public:Set(){//创建一个头结点L=(list)new(Node);L->next=NULL;//存储下一结点信息L->data=0;//头结点存储集合元素总数}~Set()//析构函数,清空所有数据所占内存{clear();}voidShowElement(){ptrNodep=L;if(!L->next){//如果头结点的指针域为空,则集合为空cout<<"集合为空!"<<endl;return;}cout<<"元素为:";while(p->next){p=p->next;cout<<p->data<<"";}cout<<endl;}Setcopy()const{//得到一个与自身元素相同的类SetR;ptrNodep=L;while(p->next){p=p->next;(p->data);}returnR;}intsize();boolIsEmpty();boolIsElement(inte)const;boolIsSubset(constSet&s)const;boolIsEqual(constSet&s)const;Set&insert(inte);Set&deletElement(inte);SetUnion(constSet&s)const;Setintersection(constSet&s)const;Setdifference(constSet&s)const;Set&clear();intgetMax();intgetMin();Set&upList();Set&downList();};intSet::size(){returnL->data;}boolSet::IsEmpty(){if(L->next)//如果头结点的指针域不为空,则集合不为空returnfalse;elsereturntrue;}boolSet::IsElement(inte)const{ptrNodep=L;while(p->next){//从第二个结点遍历所有元素,找到则返回truep=p->next;if(p->data==e)returntrue;}returnfalse;}boolSet::IsSubset(constSet&s)const{ptrNodep=;while(p->next){//依次判断s中的元素是否存在,只要有一个不存在则返回falsep=p->next;if(!IsElement(p->data))returnfalse;}returntrue;}boolSet::IsEqual(constSet&s)const{if(L->data!=->data)returnfalse;if(IsSubset(s))//元素个数相等且是其子集,则两集合相等returntrue;returnfalse;}Set&Set::insert(inte){ptrNodep;if(!IsElement(e)){//如果新元素没有重复,则创建新结点插入头结点之后p=(ptrNode)new(Node);p->data=e;p->next=L->next;L->next=p;L->data++;}return*this;}Set&Set::deletElement(inte){inttemp=L->data;//记录删除前元素个数ptrNodep=L;while(p->next){if(p->next->data==e){ptrNodeq=p->next;//p指向要删除的结点p->next=q->next;//将q所指结点信息传递给上一个结点delete(q);L->data--;break;}p=p->next;}if(temp==L->data)//如果元素个数没变,则说明元素不存在集合中cout<<"元素不存在,未删除!"<<endl;return*this;}SetSet::Union(constSet&s)const{SetR=copy();//复制本集合ptrNodep;p=;while(p->next){p=p->next;if(!IsElement(p->data)){//(p->data);}}returnR;}SetSet::intersection(constSet&s)const{Se

c++集合类 来自淘豆网www.taodocs.com转载请标明出处.

相关文档 更多>>
非法内容举报中心
文档信息
  • 页数13
  • 收藏数0 收藏
  • 顶次数0
  • 上传人875845154
  • 文件大小0 KB
  • 时间2016-03-01