64,637
社区成员
发帖
与我相关
我的任务
分享
Set& Set::operator+(Set& A)
{
//申请一个节点的内存空间nodea
//用A的data赋值给nodea
//将nodea放到这个set环中
}
#include "iostream"
using namespace std;
struct Node
{
int data;
struct Node* prior;
struct Node* next;
};
class Set;
ostream& operator<<(ostream&,Set&);
class Set
{
friend ostream& operator<<(ostream&,Set&);
public:
Set();
void Set::Clear_set();
Set& Set::operator+=(Set&);
void Set::Creat_set();
~Set();
private:
Node* head;
void addNode(int data);
};
void Set::addNode(int data)
{
Node *p=new Node;
p->data=data;
if(head==NULL)
{
head=p->prior=p->next=p;
}
else
{
head->prior->next=p;
p->next=head;
p->prior=head->prior;
head->prior=p;
}
}
Set::~Set()
{
Node *p=head, *t;
if(p!=NULL)
{
do{
t=p->next;
delete p;
p=t;
}while(p!=head);
}
}
Set::Set()
{
head = NULL;
}
void Set::Clear_set()
{
if(head!=NULL)
{
Node *p,*t;
p=t=head;
while(true)
{
t=p->next;
delete p;
p=t;
if(p==head)break;
}
head=NULL;
}
}
Set& Set::operator+=(Set& A)
{
Node *p=A.head;
if(p!=NULL)
{
Node *t=p;
do{
addNode(p->data);
p=p->next;
}while(t!=p);
}
return *this;
}
ostream& operator<<(ostream& out,Set& T)
{
Node *p = T.head, *t=p;
if(p!=NULL)
{
out<<"[ ";
do{
out<<p->data<<" ";
p = p->next;
}while(p!=t);
out<<"]";
}
return out;
}
void Set::Creat_set()
{
int d;
while(true)
{
cin>>d;
if(d==-1)break;
addNode(d);
}
}
int main()
{
Set A,B;
cout<<"请输入 A集合 的数据,-1结束:\n";
A.Creat_set();
cout<<"请输入 B集合 的数据,-1结束:\n";
B.Creat_set();
cout<<"A = "<<A<<endl;
cout<<"B = "<<B<<endl;
A += B;
cout<<"A + B = "<<A<<endl;
system("pause");
return 0;
}
运行结果:
请输入 A集合 的数据,-1结束:
16 28 -1
请输入 B集合 的数据,-1结束:
66 88 99 -1
A = [ 16 28 ]
B = [ 66 88 99 ]
A + B = [ 16 28 66 88 99 ]
请按任意键继续. . .
void Set::copy_node(const Set& obj)
{
Node* t=head;
Node* tmp=obj.head->next;
while(tmp != obj.head)
{
t->next=new Node(tmp->data);
t->next->prior=t;
t=t->next;
tmp=tmp->next;
}
t->next=head;
head->prior=t;
}
//#include "stdafx.h"
#include <iostream>
using namespace std;
struct Node
{
int data;
struct Node* prior;
struct Node* next;
Node(int a=0):data(a),prior(0),next(0) {}
};
class Set;
ostream& operator<<(ostream&,Set&);
class Set
{
friend ostream& operator<<(ostream&,Set&);
public:
Set();
Set(const Set&); //复制构造函数
~Set()
{
Clear_set();
delete head;
}
Set operator+(const Set&); //重载+
Set& operator+=(const Set&); //+=
Set& operator=(const Set&); //赋值操作符
Node* Creat_set();
void clear() //清空双链表
{
Clear_set();
head->prior = head->next = head;
}
bool isEmpty()const //空表验证
{
return head==head->next;
}
private:
Node* head;
void copy_node(const Set&); //节点复制
void Clear_set(); //节点删除
Set& insert(Set&); //插入一个表,实参将变为空表
};
Set::Set() //初始化表头
{
head = new Node;
head->next = head->prior =head;
}
void Set::Clear_set()
{
Node* p = head->next ;
while(p != head)
{
p=p->next;
delete p->prior;
}
}
void Set::copy_node(const Set& obj)
{
if(obj.isEmpty())
{
head->next = head->prior =head;
return;
}
Node* tmp=obj.head->next->next;
head->next=new Node(obj.head->next->data);
head->next->prior=head;
Node* t=head->next;
while(tmp != obj.head)
{
t->next=new Node(tmp->data);//Node类中增设构造函数
t->next->prior=t;
t=t->next;
tmp=tmp->next;
}
t->next=head;
head->prior=t;
}
Set::Set(const Set& obj)
{
this->head=new Node();//
copy_node(obj);
}
Set& Set::operator=(const Set& obj)
{
if(this!=&obj)
{
Clear_set();
copy_node(obj);
}
return *this;
}
Set& Set::operator+=(const Set& obj)
{
Set tmp(obj);
return insert(tmp);
}
Set& Set::insert(Set& A)//A对象直接加入链表 A变为空链表
{
Node* p = head;
Node* q = A.head;
p->prior->next = q->next;
q->next->prior = p->prior;
head->prior = q->prior;
q->prior->next = head;
q->next=q->prior=q; //A已清空
return *this;
}
Set Set::operator+(const Set& obj)
{
Set tmp(*this);//Set类中增设相应的构造函数
Set t(obj);
return tmp.insert(t);//insert的形参为非const引用,实参不能为临时变量
}
ostream& operator<<(ostream& out,Set& T)
{
Node* p = T.head->next;
while(p!=T.head)
{
out<<p->data<<" ";
p = p->next;
}
return out;
}
Node* Set::Creat_set()
{
Node* s=NULL;
Node* p = head;
//s = new Node;
cout<<"请输入数据(-1结束输入):";
cin>>head->data; //空着也是空着
while(-1!=head->data)
{
s=new Node(head->data);
s->prior = p->prior;
p->prior->next = s;
p->prior = s;
s->next = p;
cin>>head->data;
}
return head;
}
int main()
{
Set A,B,C;
A.Creat_set();
cout<<"A = { "<<A<<"}"<<endl;
B.Creat_set();
cout<<"B = { "<<B<<"}"<<endl;
C = A + B;
cout<<"A + B = { "<<C<<"}"<<endl;
A += C;
cout<<"A = { "<<A<<"}"<<endl;
//system("pause");
return 0;
}
Set Set::operator+(const Set& obj)
{
Set tmp(*this);//Set类中增设相应的构造函数
Set t(obj);
return tmp.insert(t); //因insert的形参不是常引用
}
Set::Set(const Set& obj)
{
this->head=new Node();//
Node* tmp=obj.head->next;
Node* t=this->head;
while(tmp != obj.head)
{
t->next=new Node(tmp->data);//Node类中增设构造函数
t->prior =t;
t=t->next; tmp=tmp->next;
}
t->next=this->head;
}
Set& Set::insert(Set& A)//A对象直接加入链表,独立在外的A不复存在
{
Node* p = head;
Node* q = A.head;
p->prior->next = q->next;
q->next->prior = p->prior;
head->prior = A.head->prior;
A.head->prior->next = head;
delete A.head;//!
return *this;
}
然后在Set类中增加自定义的复制构造函数
Set::Set(const Set& obj)
{
Node* tmp=obj.head->next,*t=this->head;
while(tmp != obj.head)
{
t->next=new Node(tmp->data);//Node类中增设构造函数
t->prior =t;
t=t->next; tmp=tmp->next;
}
t->next=this->head;
}
最后重载+
Set Set::operator+(const Set& obj)
{
Set tmp(*this);//Set类中增设相应的构造函数
return tmp.insert(Set(obj));
}
Set::~Set()
{
Node* tmp = head->next;
while(tmp != head)
{
head->next = tmp->next;
tmp->next->prior = head;
delete tmp;
tmp = head->next;
}
delete head;
}
但是有一个问题,这个函数
Set& Set::operator+(Set& A)
这个函数式是把两个set拼接到一起,你析构一个set的时候把另一个set的节点也给释放啦,所以应该重新写这个函数Set& Set::operator+(Set& A),使得两个set不要有共同的节点,从而析构的时候就会很容易处理,只需要把自己的节点释放就行啦。