下面C++程序是表示一个用链表解决两个集合的并集和差集,但就是运行不了,求高手完整解答(带结果)
#include<iostream>usingnamespacestd;classlist;classlistnode{friendclasslist;private:i...
#include<iostream>
using namespace std;
class list;
class listnode
{
friend class list;
private:
int data;
listnode* link;
public:
int getdata(){return data;}
void setdata(int value){data=value;}
listnode* getlink(){return link;}
void setlink(listnode* p){link=p;}
listnode(){link=NULL;}
listnode(int value):data(value),link(NULL){};
};
class list
{
private:
listnode* first;
public:
list(){first=new listnode();}
list(const list& r);
listnode* gethead(){return first;}
friend istream& operator >>(istream &,list&);
friend ostream& operator<<(ostream&,list&);
friend list operator +(list pa,list pb);
friend list operator -(list pa,list pb);
};
list::list(const list& r)
{
first=new listnode();
listnode* des=first->getlink(),*src=r.first->getlink();
while(src!=NULL)
{
des->setdata(src->getdata());
src=src->getlink();
des=des->getlink();
};
};
istream& operator>>(istream& in,list& inlist)
{
int a;
listnode* q=inlist.gethead( );
while(1)
{
cout<<"please input the number and ends up with -1: "<<endl;
in>>a;
if(a==-1)break;
listnode* p=new listnode(a);
q->setlink(p);
q=p;
}
return in;
};
ostream& operator<<(ostream& out,list& outlist)
{
cout<<"the result is:"<<endl;
listnode* p=outlist.first->getlink();
while(p!=NULL)
{
out<<p->getdata()<<" ";
}
out<<endl;
return out;
};
list operator+(list pa,list pb)
{
listnode* p,*q;
p=pa.gethead()->getlink();
while(p!=NULL)
{
q=pb.gethead()->getlink();
while(q!=NULL&&p->getdata()!=q->getdata())
{
q=q->getlink();
}
if(q==NULL)
{
listnode*s=new listnode(p->getdata());
s->setlink(pb.gethead()->getlink());
pb.gethead()->setlink(s);
}
p=p->getlink();
}
return pb;
};
list operator-(list pa,list pb)
{
listnode* p,*q,*s;
p=pb.gethead()->getlink();
while(p!=NULL)
{
q=pa.gethead()->getlink();
while(q!=NULL&&p->getdata()!=q->getdata())
{
s=q;
q=q->getlink();
}
if(q!=NULL)
{
s->setlink(q->getlink());
delete q;
}
p=p->getlink();
}
return pa;
};
int main()
{
list pa,pb;
cout<<"input pa:"<<endl;
cin>>pa;
cout<<"input pb:"<<endl;
cin>>pb;
list pd(pa-pb);
cout<<"the result of the multiplication is:"<<endl;
cout<<pd;
list pc(pa+pb);
cout<<"the result of the wide combine is:"<<endl;
cout<<pc;
return 0;
} 展开
using namespace std;
class list;
class listnode
{
friend class list;
private:
int data;
listnode* link;
public:
int getdata(){return data;}
void setdata(int value){data=value;}
listnode* getlink(){return link;}
void setlink(listnode* p){link=p;}
listnode(){link=NULL;}
listnode(int value):data(value),link(NULL){};
};
class list
{
private:
listnode* first;
public:
list(){first=new listnode();}
list(const list& r);
listnode* gethead(){return first;}
friend istream& operator >>(istream &,list&);
friend ostream& operator<<(ostream&,list&);
friend list operator +(list pa,list pb);
friend list operator -(list pa,list pb);
};
list::list(const list& r)
{
first=new listnode();
listnode* des=first->getlink(),*src=r.first->getlink();
while(src!=NULL)
{
des->setdata(src->getdata());
src=src->getlink();
des=des->getlink();
};
};
istream& operator>>(istream& in,list& inlist)
{
int a;
listnode* q=inlist.gethead( );
while(1)
{
cout<<"please input the number and ends up with -1: "<<endl;
in>>a;
if(a==-1)break;
listnode* p=new listnode(a);
q->setlink(p);
q=p;
}
return in;
};
ostream& operator<<(ostream& out,list& outlist)
{
cout<<"the result is:"<<endl;
listnode* p=outlist.first->getlink();
while(p!=NULL)
{
out<<p->getdata()<<" ";
}
out<<endl;
return out;
};
list operator+(list pa,list pb)
{
listnode* p,*q;
p=pa.gethead()->getlink();
while(p!=NULL)
{
q=pb.gethead()->getlink();
while(q!=NULL&&p->getdata()!=q->getdata())
{
q=q->getlink();
}
if(q==NULL)
{
listnode*s=new listnode(p->getdata());
s->setlink(pb.gethead()->getlink());
pb.gethead()->setlink(s);
}
p=p->getlink();
}
return pb;
};
list operator-(list pa,list pb)
{
listnode* p,*q,*s;
p=pb.gethead()->getlink();
while(p!=NULL)
{
q=pa.gethead()->getlink();
while(q!=NULL&&p->getdata()!=q->getdata())
{
s=q;
q=q->getlink();
}
if(q!=NULL)
{
s->setlink(q->getlink());
delete q;
}
p=p->getlink();
}
return pa;
};
int main()
{
list pa,pb;
cout<<"input pa:"<<endl;
cin>>pa;
cout<<"input pb:"<<endl;
cin>>pb;
list pd(pa-pb);
cout<<"the result of the multiplication is:"<<endl;
cout<<pd;
list pc(pa+pb);
cout<<"the result of the wide combine is:"<<endl;
cout<<pc;
return 0;
} 展开
展开全部
大致看了下。发现了一些问题 列举一下。
有问题可以直接 百度HI联系我。晚安!!
#include<iostream>
using namespace std;
/*
可以用一个类来实现。节点可以包含在link中。不需要再listnode再实现 ->next;
*/
class list;
class listnode
{
friend class list;
private:
int data;
listnode* link;
public:
int getdata()
{
return data;
}
void setdata(int value)
{
data=value;
}
listnode* getlink(){return link;}
void setlink(listnode* p){link=p;}
listnode(){link=NULL;}
listnode(int value):data(value),link(NULL){};
};
/*没有析构函数*/
class list
{
private:
listnode* first;
public:
list()
{
first=new listnode();
}
~list()
{
//删除链表所有节点
}
list(const list& r);
listnode* gethead(){return first;}
friend istream& operator >>(istream &,list&);
friend ostream& operator<<(ostream&,list&);
friend list operator +(list pa,list pb);
friend list operator -(list pa,list pb);
};
/* 复制构造函数
新复制的链表节点没有分配内存
这是错误的决定。
*/
list::list(const list& r)
{
first=new listnode();
listnode* des=first->getlink();
listnode* src=r.first->getlink();
while(src!=NULL)
{
des->setdata(src->getdata());
src=src->getlink();
des=des->getlink();
}
}
istream& operator>>(istream& in,list& inlist)
{
int a;
listnode* q=inlist.gethead( );
while(1)
{
cout<<"please input the number and ends up with -1: "<<endl;
in>>a;
if(a==-1)break;
listnode* p=new listnode(a);
q->setlink(p);
q=p;
}
return in;
}
/* new add p = p->getlink(); 不然死循环 循环输出头结点 */
ostream& operator<<(ostream& out,list& outlist)
{
cout<<"the result is:"<<endl;
listnode* p=outlist.first->getlink();
while(p!=NULL)//
{
out<<p->getdata()<<" ";
p = p->getlink();// new
}
out<<endl;
return out;
}
/*
对重载 - + 运算符 首先首先实现一个链表查找函数 const listnode* find(const int&);
差集 、并集 都应该是新的链表。不该操作就链表。
link pc;
if [ pa->a exist pb ] skip;
else
new [listnode]->pc;
*/
list operator+(list pa,list pb)
{
listnode* p,*q;
p=pa.gethead()->getlink();
while(p!=NULL)
{
q=pb.gethead()->getlink();
while(q!=NULL&&p->getdata()!=q->getdata())
{
q=q->getlink();
}
if(q==NULL)
{
listnode*s=new listnode(p->getdata());
s->setlink(pb.gethead()->getlink());
pb.gethead()->setlink(s);
}
p=p->getlink();
}
return pb;
}
list operator-(list pa,list pb)
{
listnode* p,*q,*s;
p=pb.gethead()->getlink();
/**/
while(p!=NULL)
{
q=pa.gethead()->getlink();
while(q!=NULL&&p->getdata()!=q->getdata())
//link find(const int & value)
{
s=q;
q=q->getlink();
}
if(q!=NULL)
{
s->setlink(q->getlink());
delete q;//这里删除一个节点,会改变原有链表。此举不妥
}
p=p->getlink();
}
return pa;
}
int main()
{
list pa,pb;
cout<<"input pa:"<<endl;
cin>>pa;
cout<<"input pb:"<<endl;
cin>>pb;
list pd(pa-pb);
cout<<"the result of the multiplication is:"<<endl;
cout<<pd;
list pc(pa+pb);
cout<<"the result of the wide combine is:"<<endl;
cout<<pc;
return 0;
}
展开全部
list pa,pb;
cout<<"input pa:"<<endl;
cin>>pa;
cout<<"input pb:"<<endl;
cin>>pb;
==============链表可以这样输出吗?后面有相同的问题,
cout<<"input pa:"<<endl;
cin>>pa;
cout<<"input pb:"<<endl;
cin>>pb;
==============链表可以这样输出吗?后面有相同的问题,
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
标记,明天帮你
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询