链表重复元素的统计

有三个非递减的链表,要求将重复的元素重新置于a链表中,并且其复杂度为O(n+M+P)

  1. #include <iostream>
  2. #include <list>
  3. using namespace std;
  4. void main()
  5. {
  6.     int a[10]={1,7,8,8,9,9,11,14,15,19};
  7.     int b[8]={0,1,5,7,11,15,15,19};
  8.     int c[9]={1,1,6,7,7,9,11,15,19};
  9.     list<int> a_list(a,a+10),b_list(b,b+8),c_list(c,c+9);//链表赋值的方法
  10.     list<int>::iterator a_it=a_list.begin(),b_it=b_list.begin(),c_it=c_list.begin();//迭代器初始化
  11.     while(a_it!=a_list.end() && b_it!=b_list.end() && c_it!=c_list.end())
  12.     {
  13.         list<int>::iterator temp_it;
  14.         int temp=*a_it;
  15.         if(temp>*b_it)
  16.         {
  17.             b_it++;
  18.         }
  19.         if(temp>*c_it)
  20.         {
  21.             c_it++;
  22.         }
  23.         if(temp<*b_it || temp<*c_it)
  24.         {
  25.             temp_it=a_it;
  26.             a_it++;
  27.             a_list.erase(temp_it,a_it);
  28.         }
  29.         if(temp==*b_it&&temp==*c_it)
  30.         {
  31.             int temp_1=*(--b_it);
  32.             int temp_2=*(--c_it);
  33.             if(temp!=temp_1||temp!=temp_2)//如果重复则删除元素
  34.             {
  35.                 a_it++;
  36.                 b_it++;
  37.                 b_it++;
  38.                 c_it++;
  39.                 c_it++;
  40.             }
  41.             else
  42.             {
  43.                 b_it++;
  44.                 b_it++;
  45.                 c_it++;
  46.                 c_it++;
  47.             }
  48.         }
  49.     }
  50.     if(a_it!=a_list.end())//最后还没有到a_list的结尾,则把最后的元素全部去掉
  51.     {
  52.         a_list.erase(a_it,a_list.end());
  53.     }
  54.     for(a_it=a_list.begin();a_it!=a_list.end();a_it++)//输出元素
  55.     {
  56.         cout<<*a_it<<endl;
  57.     }
  58. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值