永发信息网

下面C++程序是表示一个用链表解决两个集合的并集和差集,但就是运行不了,求高手完整解答(带结果)

答案:3  悬赏:20  手机版
解决时间 2021-03-16 10:45
  • 提问者网友:自食苦果
  • 2021-03-15 10:50
#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;
}
最佳答案
  • 五星知识达人网友:山有枢
  • 2021-03-15 11:45
大致看了下。发现了一些问题 列举一下。
有问题可以直接 百度HI联系我。晚安!!
#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()
{
//删除链表所有节点
}
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;
}

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

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;
}
全部回答
  • 1楼网友:舍身薄凉客
  • 2021-03-15 14:08
#include #include #include #include using namespace std; int main() { int a[]={1,5,8,12,5,-5,32}; int b[]={3,5,1,-3,10}; list< int > set1(a,a+sizeof(a)/sizeof(int)); list< int > set2(b,b+sizeof(b)/sizeof(int)); list< int > result; set1.sort(); set2.sort(); //交集 set_intersection(set1.begin(),set1.end(),set2.begin(),set2.end(),back_inserter(result)); copy(result.begin(),result.end(),ostream_iterator< int >(cout," ")); cout< result.clear(); //并集 set_union(set1.begin(),set1.end(),set2.begin(),set2.end(),back_inserter(result)); copy(result.begin(),result.end(),ostream_iterator< int >(cout," ")); cout< result.clear(); //差集 set_difference(set1.begin(),set1.end(),set2.begin(),set2.end(),back_inserter(result)); copy(result.begin(),result.end(),ostream_iterator< int >(cout," ")); return 0; }
  • 2楼网友:迟山
  • 2021-03-15 12:40
list pa,pb; cout<<"input pa:"<<endl; cin>>pa; cout<<"input pb:"<<endl; cin>>pb; ==============链表可以这样输出吗?后面有相同的问题,
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯