求大神写一个析构函数!!!!!!!

flyFindyou 2012-11-27 10:36:07
#include "stdafx.h"
#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();
Set& Set::operator+(Set&);
Node* Set::Creat_set();
~Set();//怎么写啊,求救啊
private:
Node* head;
};
Set::Set()
{//初始化表头
head = new Node;
head->next = head->prior =head;
}
Set& Set::operator+(Set& 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;

return *this;
}

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;
Node* p = head;
s = new Node;
cout<<"请输入数据:";
cin>>s->data;
while(-1!=s->data)
{
s->prior = p->prior;
p->prior->next = s;
p->prior = s;
s->next = p;
s = new Node;
cin>>s->data;
}
return head;
}

int main()
{
Set A,B,C;
Node* LA;
Node* LB;
LA = A.Creat_set();
cout<<"A = { "<<A<<"}"<<endl;
LB = B.Creat_set();
cout<<"B = { "<<B<<"}"<<endl;

C = A + B;
cout<<"A + B = { "<<C<<"}"endl;
//system("pause");
return 0;
}
...全文
347 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
proorck6 2012-12-27
  • 打赏
  • 举报
回复
cjfeii 2012-12-27
  • 打赏
  • 举报
回复
引用 11 楼 qxiao2859894806 的回复:
引用 10 楼 cjfeii 的回复:按理来说,析构函数应该这样写: C/C++ code?12345678910111213141516Set::~Set(){ Node* tmp = head->next; while(tmp != head) { head->next = tmp->next; tmp->next->p……
这个很容易的,我写一下步骤吧:

Set& Set::operator+(Set& A)
{
//申请一个节点的内存空间nodea
//用A的data赋值给nodea
//将nodea放到这个set环中
}
sxldfang 2012-12-27
  • 打赏
  • 举报
回复
说明: 1、operator+ 不应该返回 Set&,为方便,这里写成了operator+= 2、若要编写 operator+,请编写 拷贝构造函数,否则会出错 3、若没有编写拷贝构造函数,要避免执行拷贝构造函数情况的发生 4、因没有编写operator=,要避免赋值操作。
derekrose 2012-12-27
  • 打赏
  • 举报
回复
单链表你会写析构函数吗?
sxldfang 2012-12-27
  • 打赏
  • 举报
回复

#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 ]
请按任意键继续. . .
sxldfang 2012-12-27
  • 打赏
  • 举报
回复
构造函数不需要new Node吧, 等会,我写个看看~~~
c_rrb 2012-11-30
  • 打赏
  • 举报
回复
copy_node写得太笨拙了,重来
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;
 }
 
flyFindyou 2012-11-29
  • 打赏
  • 举报
回复
好人一生平安~
c_rrb 2012-11-29
  • 打赏
  • 举报
回复
没仔细看,上面给出的代码错误不少,现在给出一个完整的:
//#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;
 }
 
 
c_rrb 2012-11-29
  • 打赏
  • 举报
回复
楼主是想用析构函数删除一个环状双链表。 这个函数也不完善

Set Set::operator+(const Set&  obj)
 {
     Set tmp(*this);//Set类中增设相应的构造函数
     Set t(obj);
     return tmp.insert(t); //因insert的形参不是常引用
  }
 


miliggate 2012-11-29
  • 打赏
  • 举报
回复
不要用析构函数删除节点
c_rrb 2012-11-28
  • 打赏
  • 举报
回复
这个函数不完善

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;
 }

c_rrb 2012-11-28
  • 打赏
  • 举报
回复
抛砖引玉 将你的operator+改个名字,如:

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));
 }
c_rrb 2012-11-28
  • 打赏
  • 举报
回复
感觉operator+返回一个引用不妥,是否应直接为Set
flyFindyou 2012-11-28
  • 打赏
  • 举报
回复
引用 10 楼 cjfeii 的回复:
按理来说,析构函数应该这样写: C/C++ code?12345678910111213141516Set::~Set(){ Node* tmp = head->next; while(tmp != head) { head->next = tmp->next; tmp->next->prior = head; ……
可否帮忙重写一下Set& Set::operator+(Set& A)函数,我实在不知道怎么办了~
cjfeii 2012-11-28
  • 打赏
  • 举报
回复
按理来说,析构函数应该这样写:

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不要有共同的节点,从而析构的时候就会很容易处理,只需要把自己的节点释放就行啦。
flyFindyou 2012-11-28
  • 打赏
  • 举报
回复
应该不是Creat_set()的问题,因为如果把“+”的重载函数删除,主函数也不用的话,这个程序是可以正常运行的。所以可能是析构函数或“+”重载函数的问题,但我不会调试啊,各位帮帮忙了。谢谢大家的耐心啊
hznat 2012-11-27
  • 打赏
  • 举报
回复
引用 2 楼 c_rrb 的回复:
引用 1 楼 smallnat 的回复:楼主代码编译一下: 问题: 1. 有中文的分号 2. cout<<"A + B = { "<<C<<"}"endl; 少 << 析构函数 C/C++ code?1234Set::~Set(){ delete head;} 这样不行,因Node类中没有析构函数
恩。没太注意。 另外我看 Creat_set 函数,如果第一次就输入-1,似乎s没有办法释放。楼主可以仔细考虑下。
flyFindyou 2012-11-27
  • 打赏
  • 举报
回复
可我用的是双向循环链表,不是树呀!!
flyFindyou 2012-11-27
  • 打赏
  • 举报
回复
我也不确定是析构函数的问题还是那个“+”的重载有问题,求解释啊~
加载更多回复(5)

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧