1、功能:将数据进行链式存储
2、链表是一种物理存储单元上非连续的存储结构,数据元素的逻辑顺序是通过链表中的指针链接实现的
3、链表的组成:链表由一系列结点组成
4、结点的组成:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域
5、STL中的链表是一个双向循环链表
6、由于链表的存储方式并不是连续的内存空间,因此链表list中的迭代器只支持前移和后移,属于双向迭代器
7、list的优点
采用动态存储分配,不会造成内存浪费和溢出,链表执行插入和删除操作十分方便,修改指针即可,不需要移动大量元素
8、list的缺点
链表灵活,但是空间(指针域)和时间(遍历)额外耗费较大
9、list有一个重要的性质,插入操作和删除操作都不会造成原有list迭代器的失效,这在vector中是不成立的
与vector相似
- #include
- #include
-
- void printfList(const list<int>&L)
- {
- for (list<int>::const_iterator it = L.begin(); it != L.end();it++)
- {
- cout << *it << " ";
- }
- cout << endl;
- }
-
-
- void text01()
- {
- list<int> l1;
- l1.push_back(10);
- l1.push_back(20);
- l1.push_back(30);
- l1.push_back(40);
-
- printfList(l1);
-
- list<int> l2(l1.begin(), l1.end());
- printfList(l2);
-
- list<int> l3(l2);
- printfList(l3);
-
- list<int> l4(10, 100);
- printfList(l4);
- }
与vector相似
- void text01()
- {
- list<int> l1;
- l1.push_back(10);
- l1.push_back(20);
- l1.push_back(30);
- l1.push_back(40);
- printfList(l1);
-
- list<int> l2;
- l2 = l1;
- printfList(l2);
-
- list<int> l3;
- l3.assign(l2.begin(), l2.end());
- printfList(l3);
-
- list<int> l4;
- l4.assign(10, 100);
- printfList(l4);
-
- printfList(l3);
- printfList(l4);
- l4.swap(l3);
- printfList(l3);
- printfList(l4);
- }
与vector相似
判断是否为空 --- empty
返回元素个数 --- size
重新指定个数 --- resize
- void text01()
- {
- list<int> l1;
- l1.push_back(10);
- l1.push_back(20);
- l1.push_back(30);
- l1.push_back(40);
-
- if(l1.empty())
- {
- cout << "l1为空" << endl;
- }
- else
- {
- cout << "l1不为空" << endl;
- cout << "l1的大小为:" << l1.size() << endl;
- }
-
- l1.resize(10);
- printfList(l1);
-
- l1.resize(2);
- printfList(l1);
- }
和vector相似
尾插 --- push_back
尾删 --- pop_back
头插 --- push_front
头删 --- pop_front
插入 --- insert
删除 --- erase
移除 --- remove
清空 --- clear
- void text01()
- {
- list<int> l1;
- l1.push_back(10);
- l1.push_back(20);
- l1.push_back(30);
- l1.push_front(100);
- l1.push_front(200);
- l1.push_front(300);
- printfList(l1);
-
- l1.pop_back();
- printfList(l1);
-
- l1.pop_front();
- printfList(l1);
-
- l1.insert(++l1.begin(), 1000);
- printfList(l1);
-
- l1.erase(++l1.begin());
- printfList(l1);
-
- l1.push_back(10000);
- l1.push_back(10000);
- l1.push_back(10000);
- printfList(l1);
-
- l1.remove(10000);
- printfList(l1);
-
- l1.clear();
- printfList(l1);
- }
front(); //返回第一个元素
back(); //返回最后一个元素list容器中不可以通过[]或者at方式访问数据
- void text01()
- {
- list<int> l1;
- l1.push_back(10);
- l1.push_back(20);
- l1.push_back(30);
- l1.push_back(40);
-
- //不支持at和[]方式访问数据
- //cout << l1.at(0) << endl;
- //cout << l1[0] << endl;
- cout << l1.front() << endl;
- cout << l1.back() << endl;
-
- //list容器的迭代器是双向迭代器,不支持随机访问
- list<int>::iterator it = l1.begin();
- //it=it+1; 不可以跳跃访问
- }
反转 --- reverse
排序 --- sort (成员函数)
- bool myCompare(int val1,int val2)
- {
- return val1 > val2;
- }
-
- void text01()
- {
- list<int> l1;
- l1.push_back(90);
- l1.push_back(20);
- l1.push_back(30);
- l1.push_back(70);
- printfList(l1);
-
- l1.reverse();
- printfList(l1);
-
- l1.sort();
- printfList(l1);
-
- l1.sort(myCompare);
- printfList(l1);
- }
案例描述:将Person自定义数据类型进行排序,Person中属性有姓名、年龄、身高
排序规则:按照年龄进行升序,如果年龄相同按照身高进行降序
- #include
- #include
-
- class Person
- {
- public:
- Person(string name,int age,int height)
- {
- this->m_Name = name;
- this->m_Age = age;
- this->m_Height = height;
- }
-
- string m_Name;
- int m_Age;
- int m_Height;
- };
-
- bool myCompare(Person&p1,Person&p2)
- {
- if(p1.m_Age==p2.m_Age)
- {
- return p1.m_Height > p2.m_Height;
- }
- else
- {
- return p1.m_Age < p2.m_Age;
- }
- }
-
- void text01()
- {
- list
l; -
- Person p1("aa", 35, 175);
- Person p2("bb", 45, 180);
- Person p3("cc", 40, 170);
- Person p4("dd", 25, 190);
- Person p5("ee", 35, 160);
- Person p6("ff", 35, 200);
-
- l.push_back(p1);
- l.push_back(p2);
- l.push_back(p3);
- l.push_back(p4);
- l.push_back(p5);
- l.push_back(p6);
-
- for (list
::iterator it = l.begin(); it != l.end(); it++) - {
- cout << "姓名:" << it->m_Name << " 年龄:" << it->m_Age << " 身高:" << it->m_Height << endl;
- }
- cout << endl;
-
- l.sort(myCompare);
-
- for (list
::iterator it = l.begin(); it != l.end(); it++) - {
- cout << "姓名:" << it->m_Name << " 年龄:" << it->m_Age << " 身高:" << it->m_Height << endl;
- }
- }