主题:求集合的并的函数[单链表]
我的思路是:先把集合A中的元素全部输出,然后遍历集合B的每个元素,如果B的某个元素不能够在A中找到,则输出那个元素。
我测试了下,输入A={5,6},B={3,5,7},结果一运行就显示出现故障什么的问我要不要发送,请问这是怎么回事?
以下是我编的求集合的并的函数:
void Unionl(linklist &A,linklist &B)
{
node *pA,*pB;
int temp;
pA=A.Trace(1);
pB=B.Trace(1);
cout<<"A和B的并是:";
while(pA!=NULL)
{
cout<<pA->data<<",";
pA=pA->next;
}
while(pB!=NULL)
{
temp=pB->data;
if(!A.Locate(temp)) cout<<temp<<" ";
pB=pB->next;
}
cout<<endl;
}
结点类和链表类定义为:
class node
{
public:
int data;
node* next;
node(node* pnext=NULL);
node(int item,node* pnext=NULL);
void setnext(node* p) { next=p; }
void setdata(int inpt) { data=inpt; }
~node() {}
};
class linklist
{
private:
node* head;
node* pCurrent;
public:
linklist();
~linklist();
int Length();
node* Trace(int i);
int GetCurrent();
node* Locate(int x);
void InsertAfter(int n);
int DeleteCurrent();
int Isempty();
void Clear();
node* Next();
int EndofList();
void PrintList();
};
//------------------------------------------------
node::node(node* pnext) //构造头结点
{
next=pnext;
}
node::node(int item,node* pnext) //构造非头结点
{
data=item;
next=pnext;
}
//------------------------------------------------
linklist::linklist()
{
head=pCurrent=new node();
head->next=NULL;
}
linklist::~linklist()
{
}
int linklist::Length()
{
node* p=head->next;
int len=0;
while(p!=NULL)
{
len++;
p=p->next;
}
return len;
}
node* linklist::Trace(int i)
{
if(head==NULL || i<0) return NULL;
if(i==0) { pCurrent=head; return head; }
node* p=head;
int k=0;
while(p!=NULL && k<i) { p=p->next; k++; }
pCurrent=p;
return p;
}
int linklist::GetCurrent()
{
if(pCurrent==head || pCurrent==NULL)
{
cerr<<"未取到数据值"<<endl;
return NULL;
}
return pCurrent->data;
}
node* linklist::Locate(int x)
{
pCurrent=head->next;
while(pCurrent->data!=x) { pCurrent=pCurrent->next; }
return pCurrent;
}
void linklist::InsertAfter(int n)
{
if(head->next==NULL || pCurrent==NULL)
{
node* newnode=new node(n,head->next);
head->next=pCurrent=newnode;
}
else{
node* newnode=new node(n,pCurrent->next);
pCurrent->next=newnode;
pCurrent=newnode;
}
}
int linklist::DeleteCurrent()
{
if(pCurrent==head || pCurrent==NULL)
{
cerr<<"结点不可删"<<endl;
return NULL;
}
node* p=head;
while(p->next!=pCurrent) { p=p->next; }
p->next=pCurrent->next;
int datatemp=pCurrent->data;
delete pCurrent;
pCurrent=p->next;
return datatemp;
}
int linklist::Isempty()
{
if(head->next==NULL) return 1;
else return 0;
}
void linklist::Clear()
{
pCurrent=head;
node *p,*q;
p=head->next;
while(p!=NULL)
{
q=p;
p=p->next;
delete q;
}
}
node* linklist::Next()
{
if(pCurrent==NULL)
cerr<<"已指向最后一个结点"<<endl;
else pCurrent=pCurrent->next;
return pCurrent;
}
int linklist::EndofList()
{
if(pCurrent==NULL) return 1;
else return 0;
}
void linklist::PrintList()
{
node* p=head->next;
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
我测试了下,输入A={5,6},B={3,5,7},结果一运行就显示出现故障什么的问我要不要发送,请问这是怎么回事?
以下是我编的求集合的并的函数:
void Unionl(linklist &A,linklist &B)
{
node *pA,*pB;
int temp;
pA=A.Trace(1);
pB=B.Trace(1);
cout<<"A和B的并是:";
while(pA!=NULL)
{
cout<<pA->data<<",";
pA=pA->next;
}
while(pB!=NULL)
{
temp=pB->data;
if(!A.Locate(temp)) cout<<temp<<" ";
pB=pB->next;
}
cout<<endl;
}
结点类和链表类定义为:
class node
{
public:
int data;
node* next;
node(node* pnext=NULL);
node(int item,node* pnext=NULL);
void setnext(node* p) { next=p; }
void setdata(int inpt) { data=inpt; }
~node() {}
};
class linklist
{
private:
node* head;
node* pCurrent;
public:
linklist();
~linklist();
int Length();
node* Trace(int i);
int GetCurrent();
node* Locate(int x);
void InsertAfter(int n);
int DeleteCurrent();
int Isempty();
void Clear();
node* Next();
int EndofList();
void PrintList();
};
//------------------------------------------------
node::node(node* pnext) //构造头结点
{
next=pnext;
}
node::node(int item,node* pnext) //构造非头结点
{
data=item;
next=pnext;
}
//------------------------------------------------
linklist::linklist()
{
head=pCurrent=new node();
head->next=NULL;
}
linklist::~linklist()
{
}
int linklist::Length()
{
node* p=head->next;
int len=0;
while(p!=NULL)
{
len++;
p=p->next;
}
return len;
}
node* linklist::Trace(int i)
{
if(head==NULL || i<0) return NULL;
if(i==0) { pCurrent=head; return head; }
node* p=head;
int k=0;
while(p!=NULL && k<i) { p=p->next; k++; }
pCurrent=p;
return p;
}
int linklist::GetCurrent()
{
if(pCurrent==head || pCurrent==NULL)
{
cerr<<"未取到数据值"<<endl;
return NULL;
}
return pCurrent->data;
}
node* linklist::Locate(int x)
{
pCurrent=head->next;
while(pCurrent->data!=x) { pCurrent=pCurrent->next; }
return pCurrent;
}
void linklist::InsertAfter(int n)
{
if(head->next==NULL || pCurrent==NULL)
{
node* newnode=new node(n,head->next);
head->next=pCurrent=newnode;
}
else{
node* newnode=new node(n,pCurrent->next);
pCurrent->next=newnode;
pCurrent=newnode;
}
}
int linklist::DeleteCurrent()
{
if(pCurrent==head || pCurrent==NULL)
{
cerr<<"结点不可删"<<endl;
return NULL;
}
node* p=head;
while(p->next!=pCurrent) { p=p->next; }
p->next=pCurrent->next;
int datatemp=pCurrent->data;
delete pCurrent;
pCurrent=p->next;
return datatemp;
}
int linklist::Isempty()
{
if(head->next==NULL) return 1;
else return 0;
}
void linklist::Clear()
{
pCurrent=head;
node *p,*q;
p=head->next;
while(p!=NULL)
{
q=p;
p=p->next;
delete q;
}
}
node* linklist::Next()
{
if(pCurrent==NULL)
cerr<<"已指向最后一个结点"<<endl;
else pCurrent=pCurrent->next;
return pCurrent;
}
int linklist::EndofList()
{
if(pCurrent==NULL) return 1;
else return 0;
}
void linklist::PrintList()
{
node* p=head->next;
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}