C++ 链表(list)使用简述

目录

1、有关函数的作用

2、测试用例


C++ STL 库的 list 容器是一个双向链表。包含在头文件 <list> 中

1、有关函数的作用

list 本身: 

list<type> li;

定义一个参数类型为 type 的链表
li.push_back(val)在尾节点后插入值为 val 的数据,作为新的尾节点
li.push_front(val)在头节点前插入值为 val 的数据,作为新的头节点
li.size()返回链表长度
li.begin()  li.end()返回指向 头节点/尾节点 的迭代器
li.erase(iter)擦除迭代器 iter 指向的元素,并返回该元素后的元素的迭代器
li.pop_front()删除头节点
li.pop_back()删除尾节点
li.remove(val)

删除所有值为 val 的节点

li.unique()移除数值相同的连续元素,只剩一个。 注意只有连续的相同元素才会被移除
li.clear()清空链表
li.sort()升序排列,原位操作
li.reverse()反转链表,原位操作
li.splice(iter, list)

在 iter 前插入 list

algorithm 库函数操作:

li.find(iter1, iter2, val)返回在给定范围内 val 第一次出现的迭代器

2、测试用例

#include <list>
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    int i;
    /* 初始化 */
    list<int> test;  // 定义链表
    cout << test.size() << endl << endl;  // 获取链表大小  输出:0

    /* 插入元素 */
    test.push_back(1);  // 加入元素作为尾元素
    test.push_front(2); // 加入元素作为头元素
    test.push_back(3);  // 2 1 3
    cout << test.size() << endl << endl; // 输出:3

    /* 迭代器遍历 */
    list<int>::iterator iter;  // 定义迭代器
    for(iter = test.begin(); iter != test.end(); iter++)
        cout << *iter << endl;  cout << endl; // 根据迭代器获取元素  输出: 2 1 3 

    /* 插入元素 */
    iter = find(test.begin(), test.end(), 3);  // algorithm 算法库函数
    if(iter != test.end())
        test.insert(iter, 10);  // 在元素 3 前面插入元素
    for(iter = test.begin(); iter != test.end(); iter++)
        cout << *iter << endl;  cout << endl; // 输出:2 1 10 3
    

    /* 使用迭代器删除元素 */
    iter = find(test.begin(), test.end(), 2);  // algorithm 算法库函数
    if(iter != test.end())
        cout << *test.erase(iter) << endl << endl;   // 删除元素,返回删除元素后面的元素的迭代器 输出: 1
    for(iter = test.begin(); iter != test.end(); iter++)
        cout << *iter << endl;  cout << endl; // 输出:1 10 3

    /* 删除头尾节点 */
    test.pop_front();  // 删除头节点
    for(iter = test.begin(); iter != test.end(); iter++)
        cout << *iter << endl;  // 输出:10 3
    cout << endl;
    test.pop_back();  // 删除尾节点
    for(iter = test.begin(); iter != test.end(); iter++)
        cout << *iter << endl;  cout << endl; // 输出:10

    /* 根据值删除节点 */
    test.push_back(6);
    test.push_back(10);  // 10 6 10
    test.remove(10); // 删除值为 10 的所有节点
    for(iter = test.begin(); iter != test.end(); iter++)
        cout << *iter << endl;  cout << endl; // 输出:6
    
    /* 移除数值相同的连续元素,只剩一个。 注意只有连续的相同元素才会被移除 */
    test.push_back(6);
    test.push_back(7);
    test.push_back(6);  // 6 6 7 6
    test.unique();
    for(iter = test.begin(); iter != test.end(); iter++)
        cout << *iter << endl;  cout << endl; // 输出:6 7 6
    cout << test.size() << endl;  // 输出:0

    /* 反转、排序、接合 */
    test.sort(); // 升序排序
    for(iter = test.begin(); iter != test.end(); iter++)
        cout << *iter << endl;  cout << endl; // 输出:6 6 7
    test.reverse(); // 反转
    for(iter = test.begin(); iter != test.end(); iter++)
        cout << *iter << endl;  cout << endl; // 输出:7 6 6
    
    list<int> temp({1, 2, 3}); 
    test.splice(test.begin(), temp); // 在 test.begin() 前插入 temp
    for(iter = test.begin(); iter != test.end(); iter++)
        cout << *iter << endl;  cout << endl; // 输出:1 2 3 7 6 6

    /* 清空链表 */
    test.clear();  //
    cout << test.size() << endl;  // 0

    return 0;
}

  • 1
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

地球被支点撬走啦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值