STL泛型编程-list双向链表容器

list双向链表容器

    list容器实现了双向链表的数据结构,数据元素时通过链表指针串连成逻辑意义上的的线性表,这样,对链表的任一位置的元素进行插入,删除和查找都是极快速的。

双向链表示例 (a)结点结构;(b)空的双向循环链表;(c)非空的双向循环链表

    list的每个节点都有三个域:前驱元素指针域、数据域和后继元素指针域。前驱元素指针域保存了前驱元素的首地址,数据域则是本节点的数据,后继元素指针域则保存了后继元素的首地址。list的头结点的前驱元素指针域保存的是链表中尾元素的的首地址,而list的尾节点的后继元素指针域则保存了头节点的首地址,这样就构成了一个双向循环链。

    由于list对象的节点并不要求在一段连续的内存中,所以,对于迭代器,只能通过"++"或”--“的操作将迭代器移动到后继/前驱节点元素处。而不能对迭代器进行+n或-n的操作,这点,是和vector等不同的地方。

    使用list需要声明头文件"#include<list>"。

    1. 创建list对象

        1) 创建空链表,如:

            list<int> l;

        2) 创建具有n个元素的链表,如:

           list<int> l(10);//创建具有10个元素的链表

    2. 插入元素和前向遍历

        a) 使用push_back()方法从尾部插入元素,链表自动扩张。 

        b) 使用push_front()方法从头部插入元素,链表自动扩张。

        c) 使用insert()方法往迭代器位置处插入元素,链表自动扩张。注意,因为元素位置并不是物理相连,不能使用+n或-n的迭代器操作。

#include <list>
#include <iostream>
using namespace std;

int main(int argc,char* argv[])
{
    //定义元素为整型的list对象,当前没有元素
    list<int> l;
    //在链表尾部插入新元素,链表自动扩张
    l.push_back(2);
    l.push_back(1);
    l.push_back(5);
    //在链表头部插入新元素,链表自动扩张
    l.push_front(8);
    //在任意位置插入新元素,链表自动扩张
    list<int>::iterator it;
    it=l.begin();
    it++;//注意,链表的迭代器只能进行++或--操作,而不能进行+n操作
    l.insert(it,20);
    //使用前向迭代器遍历链表
    for(it=l.begin();it!=l.end();it++)
        cout<<*it<<" ";
    cout<<endl;
    return 0;
}
/*
运行结果:
8 20 2 1 5
*/

    3. 反向遍历:使用反向迭代器reverse_iterator

    4. 删除元素

        1) 使用remove()方法删除链表中一个元素,值相同的元素都会被删除。

#include <list>
#include <iostream>
using namespace std;

int main(int argc,char* argv[])
{
    //定义元素为整型的list对象,当前没有元素
    list<int> l;
    //在链表尾部插入新元素,链表自动扩张
    l.push_back(2);
    l.push_back(1);
    l.push_back(5);
    l.push_back(1);
    list<int>::iterator it;
    //使用前向迭代器遍历链表
    for(it=l.begin();it!=l.end();it++)
        cout<<*it<<" ";
    cout<<endl;
    //删除值等于1的所有元素
    l.remove(1);
    
    for(it=l.begin();it!=l.end();it++)
        cout<<*it<<" ";
    cout<<endl;
    return 0;
}
/*
运行结果
2 1 5 1
2 5
*/

        2) 使用pop_front()方法删除链表首元素,使用pop_back()方法删除链表尾元素。

#include <list>
#include <iostream>
using namespace std;

int main(int argc,char* argv[])
{
    //定义元素为整型的list对象,当前没有元素
    list<int> l;
    //在链表尾部插入新元素,链表自动扩张
    l.push_back(2);
    l.push_back(8);
    l.push_back(1);
    l.push_back(5);
    l.push_back(1);
    list<int>::iterator it;
    //使用前向迭代器遍历链表
    for(it=l.begin();it!=l.end();it++)
        cout<<*it<<" ";
    cout<<endl;
    //删除首元素
    l.pop_front();
    //删除尾元素
    l.pop_back();
    
    for(it=l.begin();it!=l.end();it++)
        cout<<*it<<" ";
    cout<<endl;
    return 0;
}
/*
运行结果
2 8 1 5 1
8 1 5
*/

        3) 使用erase()方法删除迭代器位置上的元素。

#include <list>
#include <iostream>
using namespace std;

int main(int argc,char* argv[])
{
    //定义元素为整型的list对象,当前没有元素
    list<int> l;
    //在链表尾部插入新元素,链表自动扩张
    l.push_back(2);
    l.push_back(8);
    l.push_back(1);
    l.push_back(5);
    l.push_back(1);
    list<int>::iterator it;
    //使用前向迭代器遍历链表
    for(it=l.begin();it!=l.end();it++)
        cout<<*it<<" ";
    cout<<endl;
    //删除第2个元素(从0开始计数)
    it=l.begin();
    it++;
    it++;
    l.erase(it);
    
    for(it=l.begin();it!=l.end();it++)
        cout<<*it<<" ";
    cout<<endl;
    return 0;
}
/*
运行结果
2 8 1 5 1
2 8 5 1
*/

        4) 使用clear()方法清空链表。

    5. 元素查找(find()算法需要声明头文件”#include <algorithm>”)

#include <list>
#include <algorithm>
#include <iostream>
using namespace std;

int main(int argc,char* argv[])
{
    //定义元素为整型的list对象,当前没有元素
    list<int> l;
    //在链表尾部插入新元素,链表自动扩张
    l.push_back(2);
    l.push_back(8);
    l.push_back(1);
    l.push_back(5);
    l.push_back(1);
    list<int>::iterator it;
    //使用前向迭代器遍历链表
    for(it=l.begin();it!=l.end();it++)
        cout<<*it<<" ";
    cout<<endl;
    //使用find()查找算法在链表中查找
    it=find(l.begin(),l.end(),5);
    if(it!=l.end())//找到
        cout<<"find it"<<endl;
    else
        cout<<"not find it"<<endl;

    it=find(l.begin(),l.end(),10);
    if(it!=l.end())//找到
        cout<<"find it"<<endl;
    else
        cout<<"not find it"<<endl;
    return 0;
}
/*
运行结果
2 8 1 5 1
find it
not find it
*/

    6. 元素排序

#include <list>
#include <iostream>
using namespace std;

int main(int argc,char* argv[])
{
    //定义元素为整型的list对象,当前没有元素
    list<int> l;
    //在链表尾部插入新元素,链表自动扩张
    l.push_back(2);
    l.push_back(8);
    l.push_back(1);
    l.push_back(5);
    l.push_back(1);
    list<int>::iterator it;
    //使用前向迭代器遍历链表
    for(it=l.begin();it!=l.end();it++)
        cout<<*it<<" ";
    cout<<endl;
    l.sort();
    for(it=l.begin();it!=l.end();it++)
        cout<<*it<<" ";
    cout<<endl;
    return 0;
}
/*
运行结果
2 8 1 5 1
1 1 2 5 8
*/

    7. 剔除连续重复元素。(使用unique()方法可以剔除连续重复元素,只保留一个)

#include <list>
#include <iostream>
using namespace std;

int main(int argc,char* argv[])
{
    //定义元素为整型的list对象,当前没有元素
    list<int> l;
    //在链表尾部插入新元素,链表自动扩张
    l.push_back(2);
    l.push_back(8);
    l.push_back(1);
    l.push_back(1);
    l.push_back(1);
    l.push_back(5);
    l.push_back(1);
    list<int>::iterator it;
    //使用前向迭代器遍历链表
    for(it=l.begin();it!=l.end();it++)
        cout<<*it<<" ";
    cout<<endl;
    l.unique();
    for(it=l.begin();it!=l.end();it++)
        cout<<*it<<" ";
    cout<<endl;
    return 0;
}
/*
运行结果
2 8 1 1 1 5 1
2 8 1 5 1
*/

 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值