鱼C论坛

 找回密码
 立即注册
查看: 1158|回复: 1

[技术交流] C++旅程第16站——List

[复制链接]
发表于 2020-6-22 21:40:48 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 这是她 于 2020-6-22 21:40 编辑

Never assume that you are stuck with the way things are.Life changes,and so can you.          ——Ralph Marston
                                                                     List
功能:将数据进行链式存储
链表由一系列结点组成,是一个双向循环列表。
结点的组成:一个是存储数据的元素的数据域,另一个是存储下一个结点地址的指针域。
列表不是连续的内存空间,因此链表中的迭代器只支持前移和后移,属于双向迭代器。

list的优点:
采用动态存储分配,不会造成内存浪费和溢出。
链表执行插入和删除操作十分方便,修改指针即可,不需要移动大量元素。

list的缺点:
链表空间(指针域)和时间(遍历)额外耗费较大

  1. #include<iostream>
  2. #include<list>

  3. using namespace std;

  4. void printlist(const list<int> &L)
  5. {
  6.         for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
  7.         {
  8.                 cout << *it << " ";
  9.         }
  10.         cout << endl;
  11. }

  12. void test1()
  13. {
  14.         list<int>L1;
  15.        
  16.         L1.push_back(100);
  17.         L1.push_back(200);
  18.         L1.push_back(300);
  19.         L1.push_back(400);
  20.        
  21.         printlist(L1);
  22.        
  23.         //区间方式构造
  24.         list<int>L2(L1.begin(),L1.end());
  25.         printlist(L2);
  26.        
  27.         //拷贝构造
  28.         list<int>L3(L2);
  29.         printlist(L3);
  30.        
  31.         //n个元素
  32.         list<int>L4(10,999999);
  33.         printlist(L4);
  34. }

  35. void test2()
  36. {
  37.         list<int>L1;
  38.        
  39.         L1.push_back(111);
  40.         L1.push_back(222);
  41.         L1.push_back(333);
  42.         L1.push_back(555);
  43.        
  44.         printlist(L1);
  45.        
  46.         //operator赋值
  47.         list<int>L2;
  48.         L2 = L1;
  49.         printlist(L2);
  50.        
  51.         //assign
  52.         list<int>L3;
  53.         L3.assign(L2.begin(),L2.end());
  54.         printlist(L3);
  55.        
  56.         list<int>L4;
  57.         L4.assign(10,999);
  58.         printlist(L4);
  59. }

  60. void test3()
  61. {
  62.         list<int>L1;
  63.        
  64.         L1.push_back(111);
  65.         L1.push_back(222);
  66.         L1.push_back(333);
  67.        
  68.         list<int>L2(5,999);
  69.        
  70.         cout << "交换前:     " << endl;
  71.         printlist(L1);
  72.         printlist(L2);
  73.        
  74.         L1.swap(L2);
  75.         cout << "交换后:        " << endl;
  76.         printlist(L1);
  77.         printlist(L2);
  78. }

  79. void test4()
  80. {
  81.         list<int>L1;
  82.        
  83.         L1.push_back(111);
  84.         L1.push_back(222);
  85.         L1.push_back(333);
  86.        
  87.         printlist(L1);
  88.        
  89.         if (L1.empty())
  90.         {
  91.                 cout << "L1为空!!!!!!!!!!!" << endl;  
  92.         }
  93.         else
  94.         {
  95.                 cout << "L1不为空!!!!!!!!!!!" << endl;
  96.                 cout << "L1的元素个数为:  " << L1.size() << endl;
  97.         }
  98.        
  99.         //重新指定大小
  100.         L1.resize(10);
  101.         printlist(L1);
  102.        
  103.         L1.resize(15,9999);
  104.         printlist(L1);
  105.        
  106.         L1.resize(5);
  107.         printlist(L1);
  108. }

  109. void test5()
  110. {
  111.         list<int>L1;
  112.        
  113.         //尾插
  114.         L1.push_back(123);
  115.         L1.push_back(234);
  116.         L1.push_back(345);
  117.         L1.push_back(456);
  118.        
  119.         //头插
  120.         L1.push_front(100);
  121.         L1.push_front(200);
  122.         L1.push_front(300);
  123.        
  124.         printlist(L1);
  125.        
  126.         //尾删
  127.         L1.pop_back();
  128.         printlist(L1);
  129.        
  130.         //头删
  131.         L1.pop_front();
  132.         printlist(L1);
  133.        
  134.         //insert插入
  135.         list<int>::iterator it = L1.begin();
  136.         L1.insert(++it,9999);
  137.         printlist(L1);
  138.        
  139.         //删除
  140.         it = L1.begin();
  141.         L1.erase(it);
  142.         printlist(L1);
  143.        
  144.         //移除
  145.         L1.push_back(88888);
  146.         L1.push_back(88888);
  147.         L1.push_back(88888);
  148.         printlist(L1);
  149.         L1.remove(88888);
  150.         printlist(L1);
  151.        
  152.         //清空
  153.         L1.clear();
  154.         printlist(L1);
  155.          
  156. }

  157. void test6()
  158. {
  159.         list<int>L1;
  160.        
  161.         L1.push_back(10);
  162.         L1.push_back(20);
  163.         L1.push_back(30);
  164.         L1.push_back(40);
  165.        
  166.         //不可以用[]访问list容器中的元素
  167.         //不可以用at方式访问list容器中的元素
  168.         //原因:list本质是链表,不是用连续线性空间存取数据,迭代器也是不支持随机访问的
  169.        
  170.         cout << "第一个元素为: " << L1.front() << endl;
  171.         cout << "最后一个元素为: " << L1.back() << endl;
  172.        
  173.         //验证迭代器是不支持随机访问的
  174.         list<int>::iterator it = L1.begin();
  175.         it++;
  176.         it--;
  177.         //it = it + 1;
  178.        
  179. }

  180. void test7()
  181. {
  182.         list<int>L1;
  183.        
  184.         L1.push_back(10);
  185.         L1.push_back(20);
  186.         L1.push_back(30);
  187.         L1.push_back(40);
  188.        
  189.         cout << "反转前: " << endl;
  190.         printlist(L1);
  191.        
  192.         //反转
  193.         L1.reverse();
  194.         cout << "反转后: " << endl;
  195.         printlist(L1);
  196. }

  197. void test8()
  198. {
  199.         list<int>L1;
  200.        
  201.         L1.push_back(50);
  202.         L1.push_back(80);
  203.         L1.push_back(30);
  204.        
  205.         //排序
  206.         cout << "排序前: " << endl;
  207.         printlist(L1);
  208.        
  209.         L1.sort();//默认排序规则  从小到大 升序
  210.         cout << "排序后: " << endl;
  211.         printlist(L1);
  212.        
  213.         //所有不支持随机访问迭代器的容器,不可以用标准算法
  214.         //不支持随机访问迭代器的容器,内部会提供对应一些算法
  215.         //sort(L1.begin(),L1,end());
  216. }

  217. int main()
  218. {
  219.         test1();//list构造函数
  220.        
  221.         test2();//list赋值
  222.        
  223.         test3();//list交换
  224.        
  225.         test4();//list大小操作
  226.        
  227.         test5();//list插入和删除
  228.        
  229.         test6();//list数据存取
  230.        
  231.         test7();//list反转
  232.        
  233.         test8();//list排序
  234.         return 0;
  235. }
复制代码

                                                         

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-6-22 22:05:43 | 显示全部楼层
加油
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-30 18:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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