C++程序设计实践指导1.5求两个整数集合并集改写要求实现

时间:2021-02-07 14:47:16

改写要求1:改写为单链表结构可以对任意长度整数集合求并集

#include <cstdlib>
#include <iostream> using namespace std;
struct LinkNode
{
int data;
LinkNode* next;
};
class SET
{
public:
struct LinkNode* creat(int x[],int len);
struct LinkNode* copy(LinkNode* aHead);
int notin(int elem,LinkNode* cHead)
{
LinkNode* p;
p=cHead;
while(p)
{
if(elem==p->data)
return ;
p=p->next;
}
return ;
} void insert(LinkNode* bHead,LinkNode* cHead);
void output(LinkNode* cHead)
{
LinkNode* p=cHead->next;
while(p)
{
cout<<p->data<<"\t";
p=p->next;
}
cout<<endl;
}
}; struct LinkNode* SET::creat(int x[],int len)
{
LinkNode* pHead=new LinkNode;
pHead->next=NULL;
LinkNode* p;
p=pHead;
for(int i=;i<len;i++)
{
LinkNode* newLinkNode=new LinkNode;
newLinkNode->next=NULL;
newLinkNode->data=x[i];
p->next=newLinkNode;
p=newLinkNode;
}
return pHead;
} struct LinkNode* SET::copy(LinkNode* aHead)
{
LinkNode* cHead=new LinkNode;
cHead->next=NULL;
LinkNode* p,*r;
p=aHead->next;
r=cHead;
while(p)
{
LinkNode* newLinkNode=new LinkNode;
newLinkNode->next=NULL;
newLinkNode->data=p->data;
p=p->next;
r->next=newLinkNode;
r=newLinkNode;
}
return cHead;
} void SET::insert(LinkNode *bHead,LinkNode* cHead)
{ LinkNode* q,*s,*t;
q=bHead->next;
s=cHead->next;
while(s)
{
t=s;
s=s->next;
}
while(q)
{
if(notin(q->data,cHead)!=)
{
LinkNode* newLinkNode=new LinkNode;
newLinkNode->next=NULL;
newLinkNode->data=q->data;
t->next=newLinkNode;
t=newLinkNode;
}
q=q->next;
} } int main(int argc, char *argv[])
{
int s1[]={,,,,};
int s2[]={,,,,,};
LinkNode* aHead,*bHead,*cHead,*Head;
SET set;
aHead=set.creat(s1,sizeof(s1)/sizeof(s1[]));
bHead=set.creat(s2,sizeof(s2)/sizeof(s2[]));
cHead=set.copy(aHead);
set.insert(bHead,cHead);
set.output(cHead);
system("PAUSE");
return EXIT_SUCCESS;
}

改写要求2:对任意长度的两个整数集合求交集

#include <cstdlib>
#include <iostream> using namespace std;
struct LinkNode
{
int data;
LinkNode* next;
};
class SET
{
public:
struct LinkNode* creat(int x[],int len);
struct LinkNode* copy(LinkNode* aHead);
int notin(int elem,LinkNode* cHead)
{
LinkNode* p;
p=cHead;
while(p)
{
if(elem==p->data)
return ;
p=p->next;
}
return ;
} struct LinkNode* insert(LinkNode* bHead,LinkNode* cHead);
void output(LinkNode* cHead)
{
LinkNode* p=cHead->next;
while(p)
{
cout<<p->data<<"\t";
p=p->next;
}
cout<<endl;
}
}; struct LinkNode* SET::creat(int x[],int len)
{
LinkNode* pHead=new LinkNode;
pHead->next=NULL;
LinkNode* p;
p=pHead;
for(int i=;i<len;i++)
{
LinkNode* newLinkNode=new LinkNode;
newLinkNode->next=NULL;
newLinkNode->data=x[i];
p->next=newLinkNode;
p=newLinkNode;
}
return pHead;
} struct LinkNode* SET::copy(LinkNode* aHead)
{
LinkNode* cHead=new LinkNode;
cHead->next=NULL;
LinkNode* p,*r;
p=aHead->next;
r=cHead;
while(p)
{
LinkNode* newLinkNode=new LinkNode;
newLinkNode->next=NULL;
newLinkNode->data=p->data;
p=p->next;
r->next=newLinkNode;
r=newLinkNode;
}
return cHead;
} struct LinkNode* SET::insert(LinkNode *bHead,LinkNode* cHead)
{ LinkNode* q,*t;
LinkNode* Head=new LinkNode;
q=bHead->next;
Head->next=NULL;
t=Head;
while(q)
{
if(notin(q->data,cHead)!=)
{
LinkNode* newLinkNode=new LinkNode;
newLinkNode->next=NULL;
newLinkNode->data=q->data;
t->next=newLinkNode;
t=newLinkNode;
}
q=q->next;
}
return Head;
} int main(int argc, char *argv[])
{
int s1[]={,,,,};
int s2[]={,,,,,};
LinkNode* aHead,*bHead,*cHead,*Head;
SET set;
aHead=set.creat(s1,sizeof(s1)/sizeof(s1[]));
bHead=set.creat(s2,sizeof(s2)/sizeof(s2[]));
cHead=set.copy(aHead);
Head=set.insert(bHead,cHead);
set.output(Head);
system("PAUSE");
return EXIT_SUCCESS;
}