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

改写要求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 0;
                         p=p->next;   
                 }       
                 return 1;    
             }
             
             void insert(LinkNode* bHead,LinkNode* cHead);
             void output(LinkNode* cHead)
             {
                  LinkNode* p=cHead->next;
                  while(p)
                  {
                          cout<<p->data<<"	";
                          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=0;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)!=0)
             {
                 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[]={1,3,5,7,9};
    int s2[]={1,2,3,4,5,6};
    LinkNode* aHead,*bHead,*cHead,*Head;
    SET set;
    aHead=set.creat(s1,sizeof(s1)/sizeof(s1[0]));
    bHead=set.creat(s2,sizeof(s2)/sizeof(s2[0]));    
    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 1;
                         p=p->next;   
                 }       
                 return 0;    
             }
             
             struct LinkNode* insert(LinkNode* bHead,LinkNode* cHead);
             void output(LinkNode* cHead)
             {
                  LinkNode* p=cHead->next;
                  while(p)
                  {
                          cout<<p->data<<"	";
                          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=0;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)!=0)
             {
                 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[]={1,3,5,7,9};
    int s2[]={1,2,3,4,5,6};
    LinkNode* aHead,*bHead,*cHead,*Head;
    SET set;
    aHead=set.creat(s1,sizeof(s1)/sizeof(s1[0]));
    bHead=set.creat(s2,sizeof(s2)/sizeof(s2[0]));    
    cHead=set.copy(aHead);
    Head=set.insert(bHead,cHead);
    set.output(Head);
    system("PAUSE");
    return EXIT_SUCCESS;
}
原文地址:https://www.cnblogs.com/c5395348/p/4272010.html