鱼C论坛

 找回密码
 立即注册
查看: 1951|回复: 3

两个排序后的list,求交集并集的问题

[复制链接]
发表于 2015-9-26 18:15:33 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
问题:给定两个排序后的表L1和L2,写程序用表的基本操作来实现L1和L2的交集与并集,代码如下:
  1. #include <iostream>
  2. #include <list>
  3. using namespace std;
  4. void printlist (const list<int> &s);
  5. list<int> jiao_ji (list<int> L1,list <int>L2);
  6. list<int> bing_ji (list<int> L1,list <int>L2);
  7. int main()
  8. {
  9.         int num_1[11]={1,2,3,5,7,9,10,12,17,20,23};   //int num_1[11]={1,2,3,5,7,9,10,12,17,20,23};
  10.         int num_2[8]  = {0,2,3,5,9,10,13,24};              //int num_2[8]  = {0,2,3,5,9,10,13,22};   
  11.         list <int >L1(num_1,num_1+11);
  12.         list<int> L2(num_2,num_2+8);
  13.         printlist(L1);
  14.         cout<<endl;
  15.         printlist(L2);
  16.         cout<<endl;

  17.     list <int >jiao;
  18.         list <int >bing;
  19.     jiao=jiao_ji(L1,L2);
  20.         cout<<"L1和L2的交集是:";
  21.         printlist(jiao);
  22.         cout<<endl;
  23.     bing=bing_ji(L1,L2);
  24.         cout<<"L1和L2的并集是:";
  25.         printlist(bing);
  26.         cout<<endl;
  27.         return 0;
  28. }
  29. //求交集
  30. list<int> jiao_ji (list<int> L1,list <int>L2)//[color=Red]存在一个问题,当L2先为空或者二者同时为空是就出现问题,当L1先为空时则没有问题???
  31. [/color]{
  32.         list <int >temp;
  33.                 while(!L1.empty ()&&!L2.empty ())
  34.                 {   
  35.               int k=L1.front ();
  36.                           if (k<L2.front ())
  37.                                   L1.pop_front ();
  38.                           else
  39.                           {
  40.                     while (k>=L2.front() &&!L2.empty ())
  41.                                         {
  42.                                                 if (k==L2.front ()){
  43.                                                         temp.push_back (k);
  44.                                                         L1.pop_front ();
  45.                                                         L2.pop_front ();
  46.                                                 }
  47.                                                 else
  48.                                                         L2.pop_front ();
  49.                                         }
  50.                           }
  51.                 }
  52.          return temp;     
  53. }
  54. //求并集
  55. list<int> bing_ji (list<int> L1,list <int>L2)
  56. {
  57.      list <int> temp;
  58.          while (!L1.empty ()&&!L2.empty ())
  59.          {
  60.                  int k=L1.front ();
  61.         if (k<L2.front ())
  62.                 {
  63.                         temp.push_back (k);
  64.                         L1.pop_front ();       
  65.                 }
  66.                 else if (k==L2.front ())       
  67.                 {
  68.             temp.push_back (k);
  69.                         L1.pop_front ();
  70.                         L2.pop_front ();
  71.                 }
  72.                 else
  73.                 {
  74.               while (k>L2.front ()&&!L2.empty ())
  75.                           {
  76.                                            temp.push_back (L2.front ());
  77.                                            L2.pop_front ();         
  78.                           }
  79.                 }
  80.          }
  81.        
  82.          if (L1.empty ())
  83.                  while (!L2.empty ()){
  84.                          temp.push_back (L2.front ());
  85.                          L2.pop_front ();
  86.                  }
  87.     if (L2.empty ())
  88.                  while (!L1.empty ()){
  89.                          temp.push_back (L1.front ());
  90.                          L1.pop_front ();
  91.                  }

  92.          return temp;
  93. }
  94. void printlist (const list<int> &s)
  95. {
  96.     list<int>::const_iterator  it=s.begin ();
  97.         while (it!=s.end()){
  98.      cout<<*it++<<' ';
  99.     }
  100. }
复制代码

问题就是在求交集是表L1先为空是就正常,而当L2先为空或者同时为空时则出现debug assertion failed错误,例如我在两个整型数组后面注释的那样的数组就不行,请各位大神显灵 啊啊啊啊啊啊啊……
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-9-26 18:31:00 | 显示全部楼层

回帖奖励 +1 鱼币

表示看不懂c++
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-9-26 20:26:59 | 显示全部楼层

好吧,继续坐等………………thanks
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-9-27 23:36:44 | 显示全部楼层
不懂也看看
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-21 05:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表