追忆lh 发表于 2015-9-26 18:19:27

求两个表(list)交集并集的问题

问题:给定两个排序后的表L1和L2,写程序用表的基本操作来实现L1和L2的交集与并集,代码如下:


#include <iostream>
#include <list>
using namespace std;
void printlist (const list<int> &s);
list<int> jiao_ji (list<int> L1,list <int>L2);
list<int> bing_ji (list<int> L1,list <int>L2);
int main()
{
        int num_1={1,2,3,5,7,9,10,12,17,20,23};   //int num_1={1,2,3,5,7,9,10,12,17,20,23};
        int num_2= {0,2,3,5,9,10,13,24};            //int num_2= {0,2,3,5,9,10,13,22};   
        list <int >L1(num_1,num_1+11);
        list<int> L2(num_2,num_2+8);
        printlist(L1);
        cout<<endl;
        printlist(L2);
        cout<<endl;

    list <int >jiao;
        list <int >bing;
    jiao=jiao_ji(L1,L2);
        cout<<"L1和L2的交集是:";
        printlist(jiao);
        cout<<endl;
    bing=bing_ji(L1,L2);
        cout<<"L1和L2的并集是:";
        printlist(bing);
        cout<<endl;
        return 0;
}
//求交集
list<int> jiao_ji (list<int> L1,list <int>L2)//存在一个问题,当L2先为空或者二者同时为空是就出现问题,当L1先为空时则没有问题???
{
      list <int >temp;
                while(!L1.empty ()&&!L2.empty ())
                {   
            int k=L1.front ();
                          if (k<L2.front ())
                                  L1.pop_front ();
                          else
                          {
                  while (k>=L2.front() &&!L2.empty ())
                                        {
                                                if (k==L2.front ()){
                                                        temp.push_back (k);
                                                        L1.pop_front ();
                                                        L2.pop_front ();
                                                }
                                                else
                                                        L2.pop_front ();
                                        }
                          }
                }
         return temp;   
}
//求并集
list<int> bing_ji (list<int> L1,list <int>L2)
{
   list <int> temp;
       while (!L1.empty ()&&!L2.empty ())
       {
               int k=L1.front ();
      if (k<L2.front ())
                {
                        temp.push_back (k);
                        L1.pop_front ();       
                }
                else if (k==L2.front ())       
                {
            temp.push_back (k);
                        L1.pop_front ();
                        L2.pop_front ();
                }
                else
                {
            while (k>L2.front ()&&!L2.empty ())
                          {
                                           temp.push_back (L2.front ());
                                           L2.pop_front ();       
                          }
                }
       }
       
       if (L1.empty ())
               while (!L2.empty ()){
                       temp.push_back (L2.front ());
                       L2.pop_front ();
               }
    if (L2.empty ())
               while (!L1.empty ()){
                       temp.push_back (L1.front ());
                       L1.pop_front ();
               }

       return temp;
}
void printlist (const list<int> &s)
{
    list<int>::const_iteratorit=s.begin ();
        while (it!=s.end()){
   cout<<*it++<<' ';
    }
}

问题就是在求交集是表L1先为空是就正常,而当L2先为空或者同时为空时则出现debug assertion failed错误,例如我在两个整型数组后面注释的那样的数组就不行,请各位大神显灵 啊啊啊啊啊啊啊……

战w9diwdo 发表于 2015-9-29 15:18:28

哥们,一个优秀的程序员注释含量应在40%以上,你写好注释再来问吧,太难看了
页: [1]
查看完整版本: 求两个表(list)交集并集的问题