zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    @EnableCaching缓存
    totastmessage 触发事件后浮框消失的方法
    JavaScript的类型自动转换样例集合处
    [译]bootstrap-select (selectpicker)方法
    通过使用CSS字体阴影效果解决hover图片时显示文字看不清的问题
    [Java]求文件大小并保留两位小数(文件大小是一个长整型数单位是Byte)
    PHP多进程编程(2):管道通信
    PHP多进程编程(一)
    如何解决PHP里大量数据循环时内存耗尽的问题
    推荐!国外程序员整理的 PHP 资源大全
  • 原文地址:https://www.cnblogs.com/c5395348/p/4272010.html
Copyright © 2011-2022 走看看