链表....

lovelylan9510 2008-12-12 09:31:48
能不能再帮我看看这里哪错了...运行到一半被终止了....极其的郁闷...每次到这里都错....
#include <iostream>
using namespace std;
struct Node
{
int content;
Node *next;
};
class Set
{
Node node;
Node* head;
public:
Set()
{
head=NULL;
}
Set(const Set& s)//注意拷贝构造函数的定义!!!
{
Node *p=s.head;
Node *q;//=head; head此时没有初始化!
if(this==&s) return;
head = NULL;
for (; p != NULL; p = p->next)
{
if (head == NULL)
{
q = head = new Node;
q->content = p->content;
q->next = NULL;
}
else
{
q->next = new Node;
q = q->next;
q->content = p->content;
q->next = NULL;
}
}
q=NULL;
}
~Set()
{
Node *q=head;
while (q)
{
Node* del = q;//这里要注意,不能直接delete q,否则q=q->next就没意义了。
q = q->next;
delete del;
}
}
int count()//计算这个集合中有多少个元素;
{
if(head==NULL)
return 0;
else
{ Node *p=head;
int count=0;
for(;p!=NULL;p=p->next)
count++;
return count;
}
}
bool in_set(int a)//判断元素a是否在集合里;
{
if(head==NULL)
return false;
Node *p=head;
for(;p!=NULL;p=p->next)
if(p->content==a)
return true;
return false;
}
Set& operator += (int a)
{
Node *p=new Node;
p->content=a;
p->next=head;
head=p;
return *this;
}
Set& operator -= (int a)
{
if (head->content==a)
{ Node *p=head;
head=head->next;
delete p;
}
else
{ Node *p=head;
Node *q=head->next;
for(;q!=NULL;p=p->next,q=q->next)
{ if(q->content==a)
{ p->next=q->next;
delete q;
}
break;
}
}
return *this;
}
bool operator <= (Set& s)
{
if(count()>s.count())
return false;
Node *q=head;
for(;q!=NULL;q=q->next)
if ( !s.in_set(q->content) )
break;
if(q==NULL)
return true;
else
return false;
}
bool operator ==(Set& s)
{
return (*this<=s && s<=*this);
}
bool operator !=(Set& s)
{
return (!(*this<=s && s<=*this));
}
Set& operator | (Set& s)
{
Set bing(*this);
Node *p=s.head;
for(;p!=NULL;p=p->next)
if(!in_set(p->content))
bing+=(p->content);
return bing;
}
Set& operator & (Set& s)
{
Set jiao;
Node *p=s.head;
for(;p!=NULL;p=p->next)
if(in_set(p->content))
jiao+=(p->content);
return jiao;
}
Set& operator - (Set& s)
{
Node *p=s.head;
for(;p!=NULL;p=p->next)
if(in_set(p->content))
*this-=(p->content);
return *this;
}
Set& insert(int e)
{
if(in_set(e))
cout<<e<<"已经在该集合中!"<<endl;
else
{ Node *p=new Node;//p必须初始化;
p->content=e;
p->next=head;
head=p;
}
return *this;
}
void display()
{
Node *p=head;
if (head==NULL)
cout<<"该集合是空集"<<endl;
else
cout<<"该集合中的元素为:"<<endl;
for(;p!=NULL;p=p->next)
cout<<p->content<<'\t';
}
};
void creat(Set& A)
{
int num,pd=2;
cin>>num;
if(num==-1)
{ cout<<"请问这个-1是代表结束吗?输入1代表结束,输入2代表是集合里的值:";
cin>>pd;
}
while(pd==2)
{ A.insert(num);
cin>>num;
if(num==-1)
{ cout<<"请问这个-1是代表结束吗?输入1代表结束,输入2代表是集合里的值:";
cin>>pd;
}
}
}
int main()
{
Set A,B;
cout<<"请输入集合A中的元素,以-1结束:"<<endl;
creat(A);
cout<<"请输入集合B中的元素,以-1结束:"<<endl;
creat(B);
if(A==B)
cout<<"A和B相等"<<endl;
else if(A<=B)
cout<<"A包含于B"<<endl;
else if(B<=A)
cout<<"B包含于A"<<endl;
else
cout<<"A和B无包含关系"<<endl;
cout<<"并集:"<<endl;
(A|B).display();
cout<<"交集:"<<endl;
(A&B).display();
cout<<"A和B的差集:"<<endl;
(A-B).display();
cout<<"B和A的差集:"<<endl;
(B-A).display();
cout<<"是否要对集合进行增加/删除操作?(y/n)";
char ch;
cin>>ch;
if(tolower(ch)=='n')
return 0;
else
{ cout<<"请选择:1.向A中增加元素 2.删除A中的元素 3.向B中增加元素 4.删除B中的元素";
int choose;
cin>>choose;
switch(choose)
{
case 1: cout<<"增加什么元素?";
int add;
cin>>add;
A+=add;
A.display;
break;
case 2: cout<<"删除什么元素?";
int del;
cin>>del;
A-=del;
A.display;
break;
case 3: cout<<"增加什么元素?";
int add1;
cin>>add1;
B+=add1;
B.display;
break;
case 4: cout<<"删除什么元素?";
int del1;
cin>>del1;
B-=del1;
B.display;
break;
default:cout<<"ERROR!"<<endl;
}
}
return 0;
}

...全文
57 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Qlaiaqu 2008-12-13
  • 打赏
  • 举报
回复
改了一点,程序写的不错,再接再厉,主要是把格式做好,还有就是基本语法,最主要的一点事要注意类的设计,比如那个我给你加的nodeCount,可以简化你的思维

#include "stdafx.h"
#include <iostream>
using namespace std;

struct Node
{
int content;
Node *next;
};

class Set
{
Node node;
Node* head;
int nodeCount;
public:
Set()
{
nodeCount = 0;
head=NULL;
}

Set(const Set& s)//注意拷贝构造函数的定义!!!
{
if(this == &s) return;
if(!s.nodeCount) return;

nodeCount = s.nodeCount;

Node *p=s.head;
Node *q;//=head; head此时没有初始化!
head = NULL;

for (; p != NULL; p = p->next)
{
if (head == NULL)
{
q = head = new Node;
q->content = p->content;
q->next = NULL;
}
else
{
q->next = new Node;
q = q->next;
q->content = p->content;
q->next = NULL;
}
}
q=NULL;
}

~Set()
{
Node *q=head;
Node* del;
while (q)
{
del = q;//这里要注意,不能直接delete q,否则q=q->next就没意义了。
q = q->next;
delete del;
}
}

int count()//计算这个集合中有多少个元素;
{
return nodeCount;
}

bool in_set(int a)//判断元素a是否在集合里;
{
if(head==NULL)
return false;
Node *p = head;
for(;p!=NULL;p=p->next)
if(p->content == a)
return true;
return false;
}

Set& operator += (int a)
{
Node *p=new Node;
p->content=a;
p->next=head;
head=p;
return *this;
}

Set& operator -= (int a)
{
if (head->content==a)
{
Node *p=head;
head=head->next;
delete p;
}
else
{
Node *p=head;
Node *q=head->next;
for(;q!=NULL;p=p->next,q=q->next)
{
if(q->content == a)
{
p->next=q->next;
delete q;
}
break;
}
}
return *this;
}

bool operator <= (Set& s)
{
if(count()>s.count())
return false;
Node *q=head;
for(;q!=NULL;q=q->next)
if ( !s.in_set(q->content) )
break;
if(q==NULL)
return true;
else
return false;
}

bool operator ==(Set& s)
{
return (*this<=s && s<=*this);
}

bool operator !=(Set& s)
{
return (!(*this<=s && s<=*this));
}

Set& operator | (Set& s)
{
Set *bing = new Set(*this);
Node *p=s.head;
for(;p!=NULL;p=p->next)
if(!in_set(p->content))
*bing+=(p->content);
return *bing;
}

Set& operator & (Set& s)
{
Set *jiao = new Set;
Node *p=s.head;
for(;p!=NULL;p=p->next)
if(in_set(p->content))
*jiao+=(p->content);
return *jiao;
}

Set& operator - (Set& s)
{
Node *p=s.head;
for(;p!=NULL;p=p->next)
{
if(in_set(p->content))
*this-=(p->content);
}
return *this;
}

Set& insert(int e)
{
if(in_set(e))
cout<<e<<"已经在该集合中!"<<endl;
else
{ Node *p=new Node;//p必须初始化;
p->content=e;
p->next=head;
head=p;
nodeCount++;
}
return *this;
}

void display()
{
Node *p=head;
if (head==NULL)
cout<<"该集合是空集"<<endl;
else
cout<<"该集合中的元素为:"<<endl;
for(;p!=NULL;p=p->next)
cout<<p->content<<'\t';
}
};
void creat(Set& A)
{
int num,pd=2;
cin>>num;
if(num==-1)
{ cout<<"请问这个-1是代表结束吗?输入1代表结束,输入2代表是集合里的值:";
cin>>pd;
}
while(pd==2)
{ A.insert(num);
cin>>num;
if(num==-1)
{ cout<<"请问这个-1是代表结束吗?输入1代表结束,输入2代表是集合里的值:";
cin>>pd;
}
}
}
int main()
{
Set A,B;
cout<<"请输入集合A中的元素,以-1结束:"<<endl;
creat(A);
cout<<"请输入集合B中的元素,以-1结束:"<<endl;
creat(B);
if(A==B)
cout<<"A和B相等"<<endl;
else if(A<=B)
cout<<"A包含于B"<<endl;
else if(B<=A)
cout<<"B包含于A"<<endl;
else
cout<<"A和B无包含关系"<<endl;
cout<<endl<<"并集:"<<endl;
(A|B).display();
cout<<endl<<"交集:"<<endl;
(A&B).display();
cout<<endl<<"A和B的差集:"<<endl;
(A-B).display();
cout<<endl<<"B和A的差集:"<<endl;
(B-A).display();
cout<<endl<<"是否要对集合进行增加/删除操作?(y/n)";
char ch;
cin>>ch;
if(tolower(ch)=='n')
return 0;
else
{ cout<<"请选择:1.向A中增加元素 2.删除A中的元素 3.向B中增加元素 4.删除B中的元素";
int choose;
cin>>choose;
switch(choose)
{
case 1: cout<<"增加什么元素?";
int add;
cin>>add;
A+=add;
A.display();
break;
case 2: cout<<"删除什么元素?";
int del;
cin>>del;
A-=del;
A.display();
break;
case 3: cout<<"增加什么元素?";
int add1;
cin>>add1;
B+=add1;
B.display();
break;
case 4: cout<<"删除什么元素?";
int del1;
cin>>del1;
B-=del1;
B.display();
break;
default:cout<<"ERROR!"<<endl;
}
}
return 0;
}





Qlaiaqu 2008-12-12
  • 打赏
  • 举报
回复
up
lann64 2008-12-12
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;
struct Node
{
int content;
Node *next;
};
class Set
{
Node node;
Node* head;
public:
Set()
{
head=NULL;
}
Set(const Set& s)//注意拷贝构造函数的定义!!!
{
Node *p=s.head;
Node *q;//=head; head此时没有初始化!
if (this==&s) return;
head = NULL;
for (; p != NULL; p = p->next)
{
if (head == NULL)
{
q = head = new Node;
q->content = p->content;
q->next = NULL;
}
else
{
q->next = new Node;
q = q->next;
q->content = p->content;
q->next = NULL;
}
}
q=NULL;
}
~Set()
{
Node *q=head;
while (q)
{
Node* del = q;//这里要注意,不能直接delete q,否则q=q->next就没意义了。
q = q->next;
delete del;
}
}
int count()//计算这个集合中有多少个元素;
{
if (head==NULL)
return 0;
else
{
Node *p=head;
int count=0;
for (;p!=NULL;p=p->next)
count++;
return count;
}
}
bool in_set(int a)//判断元素a是否在集合里;
{
if (head==NULL)
return false;
Node *p=head;
for (;p!=NULL;p=p->next)
if (p->content==a)
return true;
return false;
}
Set& operator += (int a)
{
Node *p=new Node;
p->content=a;
p->next=head;
head=p;
return *this;
}
Set& operator -= (int a)
{
if (head->content==a)
{
Node *p=head;
head=head->next;
delete p;
}
else
{
Node *p=head;
Node *q=head->next;
for (;q!=NULL;p=p->next,q=q->next)
{
if (q->content==a)
{
p->next=q->next;
delete q;
}
break;
}
}
return *this;
}
bool operator <= (Set& s)
{
if (count()>s.count())
return false;
Node *q=head;
for (;q!=NULL;q=q->next)
if ( !s.in_set(q->content) )
break;
if (q==NULL)
return true;
else
return false;
}
bool operator ==(Set& s)
{
return (*this<=s && s<=*this);
}
bool operator !=(Set& s)
{
return (!(*this<=s && s<=*this));
}
Set operator | (Set& s) //需要返回引用吗?而且还是局部变量的引用
{
Set bing(*this);
Node *p=s.head;
for (;p!=NULL;p=p->next)
if (!in_set(p->content))
bing+=(p->content);
return bing;
}
Set operator & (Set& s) //需要返回引用吗?而且还是局部变量的引用
{
Set jiao;
Node *p=s.head;
for (;p!=NULL;p=p->next)
if (in_set(p->content))
jiao+=(p->content);
return jiao;
}
Set& operator - (Set& s)
{
Node *p=s.head;
for (;p!=NULL;p=p->next)
if (in_set(p->content))
*this-=(p->content);
return *this;
}
Set& insert(int e)
{
if (in_set(e))
cout<<e<<"已经在该集合中!"<<endl;
else
{
Node *p=new Node;//p必须初始化;
p->content=e;
p->next=head;
head=p;
}
return *this;
}
void display()
{
Node *p=head;
if (head==NULL)
cout<<"该集合是空集"<<endl;
else
cout<<"该集合中的元素为:"<<endl;
for (;p!=NULL;p=p->next)
cout<<p->content<<'\t';
}
};
void creat(Set& A)
{
int num,pd=2;
cin>>num;
if (num==-1)
{
cout<<"请问这个-1是代表结束吗?输入1代表结束,输入2代表是集合里的值:";
cin>>pd;
}
while (pd==2)
{
A.insert(num);
cin>>num;
if (num==-1)
{
cout<<"请问这个-1是代表结束吗?输入1代表结束,输入2代表是集合里的值:";
cin>>pd;
}
}
}
int main()
{
Set A,B;
cout<<"请输入集合A中的元素,以-1结束:"<<endl;
creat(A);
cout<<"请输入集合B中的元素,以-1结束:"<<endl;
creat(B);
if (A==B)
cout<<"A和B相等"<<endl;
else if (A<=B)
cout<<"A包含于B"<<endl;
else if (B<=A)
cout<<"B包含于A"<<endl;
else
cout<<"A和B无包含关系"<<endl;
cout<<"并集:"<<endl;
(A|B).display();
cout<<"交集:"<<endl;
(A&B).display();
cout<<"A和B的差集:"<<endl;
(A-B).display();
cout<<"B和A的差集:"<<endl;
(B-A).display();
cout<<"是否要对集合进行增加/删除操作?(y/n)";
char ch;
cin>>ch;
if (tolower(ch)=='n')
return 0;
else
{
cout<<"请选择:1.向A中增加元素 2.删除A中的元素 3.向B中增加元素 4.删除B中的元素";
int choose;
cin>>choose;
switch (choose)
{
case 1:
cout<<"增加什么元素?";
int add;
cin>>add;
A+=add;
A.display(); ////-----------
break;
case 2:
cout<<"删除什么元素?";
int del;
cin>>del;
A-=del;
A.display();//-----------
break;
case 3:
cout<<"增加什么元素?";
int add1;
cin>>add1;
B+=add1;
B.display();//--------------
break;
case 4:
cout<<"删除什么元素?";
int del1;
cin>>del1;
B-=del1;
B.display();//-----------------
break;
default:
cout<<"ERROR!"<<endl;
}
}
return 0;
}

语法错改了,程序逻辑上还有错。
jeckpc 2008-12-12
  • 打赏
  • 举报
回复
考下来,调调
呵呵~
HelloDan 2008-12-12
  • 打赏
  • 举报
回复
太长了,我没时间,你最好画一下图,找准关系。要学会用高度工具。

65,210

社区成员

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

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