list链表

打印 上一主题 下一主题

主题 1677|帖子 1677|积分 5031

概念

list概念:将数据进行链式存储
list链表:物理存储单元上非连续的存储结构,数据元素的逻辑顺序是通过链表的指针链接实现的。
存储数据元素的数据域和存储下一个节点位置的指针域

由于链表的存储方式并不是连续的内存空间,因此链表list中的迭代器只支持前移和后移,属于双向迭代器
构造函数

   list<T> lst;// 创建一个空的list
  list(beg,end);// 将[beg,end)的元素拷贝给自己
  list(n,elem);// 将n个elem拷贝给自己
  list(const list &list);// 拷贝构造函数
  1. #include <iostream>
  2. #include <vector>
  3. #include <list>
  4. using namespace std;
  5. void printList(const list<int> &L){
  6.     for(list<int>::const_iterator it=L.begin();it!=L.end();it++){
  7.         cout<<*it<<" ";// *it表示迭代器it指向的元素,解引用
  8.     }
  9.     cout<<endl;
  10. }
  11. void test(){
  12.     list<int> L1;
  13.     L1.push_back(10);
  14.     L1.push_back(20);
  15.     L1.push_back(30);
  16.     L1.push_back(40);
  17.     printList(L1);//`
  18.     list<int> L2(L1.begin(),L1.end());
  19.     printList(L2);// 10 20 30 40
  20.     list<int> L3(L2);// 拷贝构造
  21.     printList(L3);// 10 20 30 40
  22.     list<int> L4(10,100);
  23.     printList(L4);// 100 100 100 100 100 100 100 100 100 100
  24. }
  25. int main()
  26. {
  27.     test();
  28.     system("pause");
  29.     return 0;
  30. }
复制代码
list复制和交换

   assign(beg,end);//将[beg,end)区间内的元素拷贝到调用该方法的list中。
  assign(n,elem);//将n个elem拷贝到调用该方法的list中。
  list& operator=(const list &lst);//将lst拷贝到调用该方法的list中。
  swap(list);//将调用该方法的list与lst交换。
  1. #include <iostream>
  2. #include <vector>
  3. #include <list>
  4. using namespace std;
  5. void printList(const list<int> &L)
  6. {
  7.     for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
  8.     {
  9.         cout << *it << " ";
  10.     }
  11.     cout << endl;
  12. }
  13. void test1(){
  14.     list<int> L1;
  15.     L1.push_back(10);
  16.     L1.push_back(20);
  17.     L1.push_back(30);
  18.     L1.push_back(40);
  19.     list<int> L2(L1);
  20.     // L2=L1;
  21.     printList(L2);
  22.     list<int> L3;
  23.     L3.assign(L1.begin(),L1.end());
  24.     printList(L3);
  25.     list<int> L4;
  26.     L4.assign(5,100);
  27.     printList(L4);
  28. }
  29. void test2(){
  30.     list<int> L1;
  31.     L1.push_back(10);
  32.     L1.push_back(20);
  33.     L1.push_back(30);
  34.     L1.push_back(40);
  35.     list<int> L2;
  36.     L2.assign(5,200);
  37.     cout<<"交换前:"<<endl;
  38.     printList(L1);
  39.     printList(L2);
  40.     L1.swap(L2);
  41.     cout<<"交换后:"<<endl;
  42.     printList(L1);
  43.     printList(L2);
  44. }
  45. int main()
  46. {
  47.     // test1();
  48.     test2();
  49.     system("pause");
  50.     return 0;
  51. }
复制代码
list巨细操作

   size();//返回list的巨细
  empty();//判定list是否为空
  resize(num);//调整list的巨细,若小于num,则用0填充,若大于num,则删除多余的元素
  resize(num,elem);//调整list的巨细,若小于num,则用elem填充,若大于num,则删除多余的元素
  clear();//清空list
  1. #include <iostream>
  2. #include <vector>
  3. #include<list>
  4. using namespace std;
  5. void printList(const list<int>& L){
  6.     for(list<int>::const_iterator it=L.begin();it!=L.end();it++){
  7.         cout<<*it<<" ";
  8.     }
  9.     cout<<endl;
  10. }
  11. void test(){
  12.     list<int> L1;
  13.     L1.push_back(10);
  14.     L1.push_back(20);
  15.     L1.push_back(30);
  16.     L1.push_back(40);
  17.     if(L1.empty()){
  18.         cout<<"L1为空"<<endl;
  19.     }
  20.     else{
  21.         cout<<"L1不为空"<<endl;
  22.         cout<<"L1的大小为:"<<L1.size()<<endl;
  23.     }
  24.     L1.resize(8);
  25.     cout<<"调整L1的大小为10后:"<<endl;
  26.     printList(L1);//10 20 30 40 0 0 0 0
  27.     L1.resize(10,100);
  28.     cout<<"调整L1的大小为5,填充100后:"<<endl;
  29.     printList(L1);//10 20 30 40 0 0 0 0 100 100
  30. }
  31. int main()
  32. {
  33.     test();
  34.     system("pause");
  35.     return 0;
  36. }
复制代码
 list插入和删除

   push_back(elem);//在链表尾部插入一个元素
  pop_back();//在链表尾部删除一个元素
  push_front(elem);//在链表头部插入一个元素
  pop_front();//在链表头部删除一个元素
  
  insert(pos,elem);//在pos位置插入一个值为elem的节点
  insert(pos,n,elem);//在pos位置插入n个值为elem的节点
  clear();//清空链表
  erase(beg,end);//删除[beg,end)区间的节点
  erase(pos);//删除pos位置的节点
  remove(elem);//删除链表中所有值为elem的节点
  1. #include <iostream>
  2. #include <vector>
  3. #include<list>
  4. using namespace std;
  5. void printList(const list<int>& L){
  6.     for(list<int>::const_iterator it=L.begin();it!=L.end();it++){
  7.         cout<<*it<<" ";
  8.     }
  9.     cout<<endl;
  10. }
  11. void test(){
  12.     list<int> L;
  13.     //尾插
  14.     L.push_back(10);
  15.     L.push_back(20);
  16.     L.push_back(30);
  17.     //尾删
  18.     L.pop_back();
  19.     printList(L);//10 20
  20.     //头插
  21.     L.push_front(100);
  22.     L.push_front(200);
  23.     L.push_front(300);
  24.     //头删
  25.     L.pop_front();
  26.     printList(L);//200 100 10 20
  27.     //插入
  28.     list<int>::iterator it=L.begin();
  29.     L.insert(it,1000);
  30.     printList(L);//1000 200 100 10 20
  31.     //删除
  32.     it=L.begin();
  33.     L.erase(it);//删除it位置的元素
  34.     printList(L);//200 100 10 20
  35.     //移除
  36.     L.push_back(10000);
  37.     L.push_back(10000);
  38.     L.push_back(10000);
  39.     L.remove(10000);
  40.     printList(L);//200 100 10 20
  41.     //清空
  42.     L.clear();
  43.     printList(L);
  44. }
  45. int main()
  46. {
  47.     test();
  48.     system("pause");
  49.     return 0;
  50. }
复制代码
list数据存取

  1. #include <iostream>
  2. #include <vector>
  3. #include<list>
  4. using namespace std;
  5. void test(){
  6.     list<int> L;
  7.     L.push_back(100);
  8.     L.push_back(200);
  9.     L.push_back(300);
  10.     L.push_back(400);
  11.     cout<<L.front()<<endl;//100
  12.     cout<<L.back()<<endl;//400
  13.     // list容器的迭代器是双向迭代器,不支持随机访问
  14.     list<int>::iterator it=L.begin();// 迭代器it指向第一个元素
  15.     cout<<*it<<endl;//100
  16. }
  17. int main()
  18. {
  19.     test();
  20.     system("pause");
  21.     return 0;
  22. }
复制代码
list反转和排序

   reverse();// 反转
  sort();// 排序
  1. #include <iostream>
  2. #include <vector>
  3. #include<list>
  4. using namespace std;
  5. void printList(const list<int>& L){
  6.     for(list<int>::const_iterator it=L.begin();it!=L.end();it++){
  7.         cout<<*it<<" ";
  8.     }
  9.     cout<<endl;
  10. }
  11. bool myCompare(int v1,int v2){
  12.     return v1>v2;
  13. }
  14. void test(){
  15.     list<int> L1;
  16.     L1.push_back(50);
  17.     L1.push_back(30);
  18.     L1.push_back(90);
  19.     L1.push_back(40);
  20.     printList(L1);// 50 30 90 40
  21.     //反转
  22.     L1.reverse();
  23.     printList(L1);// 40 90 30 50
  24.     //排序
  25.     L1.sort();// 默认从小到大
  26.     printList(L1);// 30 40 50 90
  27.     L1.sort(myCompare);//大到小
  28.     printList(L1);// 90 50 40 30
  29. }
  30. int main()
  31. {
  32.     test();
  33.     system("pause");
  34.     return 0;
  35. }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

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

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

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

本版积分规则

大连密封材料

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表