#include
#include
using namespace std;
//整数集合类TSet(链表版)
struct Node{
int val; //具体元素的值
Node * next; //指向下一元素的指针
};
class TSet{
Node * head; //指向链表头的指针
int find(int x); //查找元素x是否在集合中(如x不存在返回-1)
void DeleteAll();//删除集合所有元素
public:
TSet(){ head=0; }
TSet(const TSet &s);
~TSet();
void insert(int x);//增加集合元素x
void Delete(int x);//删除集合元素x
void display();//输出 集合元素x
TSet operator*(const TSet & s1); //实现两个集合的交集
TSet operator-(TSet & s1); //实现两个集合的差集
TSet operator+(TSet & s1); //实现两个集合的并集
TSet& operator=(TSet & s1); //实现集合赋值运算
};
TSet::TSet(const TSet &s){
Node *p,*q,*t;
head=0;
p=s.head;
while(p){
t=new Node;
t->val=p->val;
t->next=0;
if(head==0){
head=q=t;
}else{
q->next=t;
q=t;
}
p=p->next;
}
}
TSet::~TSet(){
if(head)
DeleteAll();
}
//查找元素x是否在集合中(如x不存在返回-1)
int TSet::find(int x){
if(head==0)
return -1;
Node *p=head;
while(p!=0 && p->val!=x)
p=p->next;
return p==0 ? -1 : 0;
}
//删除集合所有元素
void TSet::DeleteAll(){
Node *p;
while(head){
p=head;
head=p->next;
delete p;
}
head=0;
}
//增加集合元素x
void TSet::insert(int x){
if(find(x)==0){
cout <<"该元素已经存在。\n";
return;
}
Node *p,*q,*t;
if(head==0){ //在空集中插入首个元素
head=new Node;
head->val=x;
head->next=0;
return;
}
p=head;
if(x
t=new Node;
t->val=x;
t->next=head;
head=t;
return;
}
//寻找插入位置q
q=p; p=p->next;
while(p!=0 && x>p->val)
q=p, p=p->next;
t=new Node;
t->val=x;
t->next=q->next;
q->next=t;
}
//删除集合元素x
void TSet::Delete(int x){
Node *p=head,*q;
if(p->val==x){ //删除的结点为首结点
head=p->next;
delete p;
return;
}
q=p;
p=p->next;
while(p!=0 && p->val!=x)
q=p, p=p->next;
if(p==0){
cout <<"该元素已经存在。\n";
return;
}
p=q->next;
q->next=p->next;
delete p;
}
//输出 集合元素x
void TSet::display(){
Node *p=head;
if(head==0){
cout <<"该集合是空集。\n";
return;
}
while(p){
cout <
p=p->next;
}
cout <
//实现两个集合的交集
TSet TSet::operator*(const TSet & s1){
TSet ts;
ts.head=0;
Node *p,*q,*h=0,*t;
p=head;
q=s1.head;
while(p && q){
if(p->val==q->val){
t=new Node;
t->val=p->val;
t->next=0;
if(h==0){
h=t, ts.head=h;
}else{
h->next=t, h=t;
}
p=p->next;
q=q->next;
}else if(p->val
p=p->next;
}else{
q=q->next;
}
}
return ts;
}
//实现两个集合的差集
TSet TSet::operator-(TSet & s1){
TSet ts;
Node *p=head,*h=0,*t;
while(p){
if(s1.find(p->val)==-1){
t=new Node;
t->val=p->val;
t->next=0;
if(h==0)
ts.head=h=t;
else
h->next=t, h=t;
}
p=p->next;
}
return ts;
}
//实现两个集合的并集
TSet TSet::operator+(TSet & s1){
TSet ts(*this);
Node *p;
int x;
for(p=s1.head; p; p=p->next){
x=p->val;
if(ts.find(x)==-1)
ts.insert(x);
}
return ts;
}
//实现集合赋值运算
TSet& TSet::operator=(TSet & s1){
if(this==&s1)
return *this;
if(head)
DeleteAll();
if(s1.head==0)
return *this;
Node *p,*q,*t;
head=new Node;
head->val=s1.head->val;
head->next=0;
p=s1.head->next;
q=head;
while(p){
t=new Node;
t->val=p->val;
t->next=0;
q->next=t;
q=t;
p=p->next;
}
return *this;
}
void main(){
TSet s1,s2,s3,s4,s5;
int i,x;
srand(time(0));
for(i=0; i<10; i++){
s1.insert(rand()%50);
s2.insert(rand()%50);
}
cout <<"s1: "; s1.display();
cout <<"s2: "; s2.display();
cin >>x;
s1.Delete(x);
cout <<"s1: "; s1.display();
s3=s1*s2;
cout <<"s3=s1*s2: "; s3.display();
s4=s1-s2;
cout <<"s4=s1-s2: "; s4.display();
s5=s1+s2;
cout <<"s5=s1+s2: "; s5.display();
}