#pragma once
#ifndef _LINKEDSET_H_
#define _LINKEDSET_H_
#include <assert.h>
template <class T>
struct SetNode //集合的节点类的定义;
{
T data;
SetNode<T> *link;
SetNode() : link(NULL) { }
SetNode(const T& x, SetNode<T> *next=NULL) : data(x), link(next) { }
};
template <class T>
class LinkedSet //集合的类定义;
{
public:
LinkedSet() {first=last=new SetNode<T>;}
LinkedSet(const LinkedSet<T>& R) //复制构造函数;
{
SetNode<T> *srcptr=R.first->link;
first=last=new SetNode<T>;
while(srcptr!=NULL)
{
last->link=new SetNode<T>(srcptr->data);
last=last->link;
srcptr=srcptr->link;
}
}
~LinkedSet() {makeEmpty(); delete first;}
bool addMember(const T& x) //把新元素x加入到集合中;
{
SetNode<T> *p=first->link, *pre=first;
while(p!=NULL&&p->data<x) {pre=p; p=p->link;}
if(p!=NULL&&p->data==x) return false;
SetNode<T> *s=new SetNode<T>(x);
assert(s!=NULL);
s->link=p; pre->link=s;
if(p==NULL) last=s;
return true;
}
bool delMember(const T& x) //把集合中成员x删去;
{
SetNode<T> *p=first->link; *pre=first;
while(p!=NULL&&p->data<x) {pre=p; p=p->link;}
if(p!=NULL&&p->data=x)
{
pre->link=p->link;
if(p==last) last=pre;
delete p;
return ture;
}
else return false;
}
bool Contains(const T& x) //判x是否为集合的成员;
{
SetNode<T> *temp=first->link;
while(temp!=NULL&&temp->data<x) temp=temp->link;
if(temp!=NULL&&temp->data==x) return true;
else return false;
}
LinkedSet<T>& operator=(LinkedSet<T>& R) //复制集合R到this;
{
SetNode<T> *pb=R.first->link;
SetNode<T> *pa=first=new SetNode<T>;
while(pb!=NULL)
{
pa->link=new SetNode<T>(pb->data);
pa=pa->link; pb=pb->link;
}
pa->link=NULL; last=pa;
return *this;
}
LinkedSet<T>& operator+(LinkedSet<T>& R) //求集合与R的并;
{
SetNode<T> *pb=R.first->link;
SetNode<T> *pa=first->link;
LinkedSet<T> temp;
SetNode<T> *p, *pc=temp.first;
while(pa!=NULL&&pb!=NULL)
{
if(pa->data==pb->data)
{
pc->link=new SetNode<T>(pa->data);
pa=pa->link; pb=pb->link;
}
else if(pa->data<pb->data)
{
pc->link=new SetNode<T>(pa->data);
pa=pa->link;
}
else
{
pc->link=new SetNode<T>(pb->link);
pb=pb->link;
}
pc=pc->link;
}
if(pa!=NULL) p=pa;
else p=pb;
while(p!=NULL)
{
pc->link=new SetNode<T>(p->data);
p=p->link; pc=pc->link;
}
pc->link=NULL; temp.last=pc;
return temp;
}
LinkedSet<T>& operator*(LinkedSet<T>& R) //求集合this与R的交集;
{
SetNode<T> *pb=R.first->link;
SetNode<T> *pa=first->link;
LinkedSet<T> temp;
SetNode<T> *pc=temp.first;
while(pa!=NULL&&pb!=NULL)
{
if(pa->data==pb->data)
{
pc->link=new SetNode<T>(pa->data);
pa=pa->link; pb=pb->link;
}
else if(pa->data<pb->data) pa=pa->link;
else pb=pb->link;
}
pc->link=NULL; temp.last=pc;
return temp;
}
LinkedSet<T>& operator-(LinkedSet<T>& R) //求集合this与R的差;
{
SetNode<T> *pb=R.first->link;
SetNode<T> *pa=first->link;
LinkedSet<T> temp;
SetNode<T> *pc=temp.first;
while(pa!=NULL&&pb!=NULL)
{
if(pa->data==pb->data) {pa=pa->link; pb=pb->link;}
else if(pa->data<pb->data)
{
pc->link=new SetNode<T>(pa->data);
pa=pa->link; pc=pc->link;
}
else pb=pb->link;
}
while(pa!=NULL)
{
pc->link=new SetNode<T>(pa->data);
pc=pc->link; pa=pa->link;
}
pc->link=NULL; temp.last=pc;
return temp;
}
bool operator==(LinkedSet<T>& R) //判断两个集合是否相等;
{
SetNode<T> *pb=R.first->link;
SetNode<T> *pa=first->link;
while(pa!=NULL&&pb!=NULL)
{
if(pa->data==pb->data) {pa=pa->link; pb=pb->link;}
else return false;
}
if(pa!=NULL||pb!=NULL) return false;
return true;
}
bool Min(T& x)
{
if(first==last) return false;
x=first->link->data;
return true;
}
bool Max(T& x)
{
if(first==last) return false;
x=last->data;
return true;
}
private:
void makeEmpty() //将链表置为空;
{
SetNode<T> *q;
while(first->link!=NULL)
{
q=first->link;
first->link=q->link;
delete q;
}
}
private:
SetNode<T> *first;
SetNode<T> *last;
};
#endif
用有序链表实现集合
最新推荐文章于 2023-03-08 23:21:31 发布