1 / 13
文档名称:

c++集合类.doc

格式:doc   页数:13页
下载后只包含 1 个 DOC 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

分享

预览

c++集合类.doc

上传人:875845154 2016/3/1 文件大小:0 KB

下载得到文件列表

c++集合类.doc

相关文档

文档介绍

文档介绍:题目:编写一个集合类,实现集合的相应运算。截图:源代码:#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