使用STL中的list容器实现单链表的操作

简介:
#include<iostream>
#include<list>
#include<algorithm>
using namespace std;
void Print(int &item)
{
  cout<<item<<" ";
}
int main()
{
  list<int> listintegers;
  list<int>::iterator listiter;  //引入迭代器

  //------------头插法插入元素-------------
  listintegers.push_front(5);
  listintegers.push_front(3);
  listintegers.push_front(1);
  listintegers.push_front(2);
  listintegers.push_front(4);

   //----------尾插法插入元素----------
  listintegers.push_back(6);
  listintegers.push_back(8);
  listintegers.push_back(7);

  //--------使用list的成员函数insert()插入元素到链表中
  listintegers.insert(listintegers.end(),10);
  listintegers.insert(listintegers.end(),9);




  //----------利用迭代器输出链表-----------
  /* 我们在每一个算法中都使用一个或多个iterator。

我们使用它们来存取容器中的对象。 要存取一个给定的对象,我们把一个iterator指向它,然后间接引用这个iterator */ cout<<"链表为:"; for(listiter=listintegers.begin();listiter!=listintegers.end();listiter++) { cout<<*listiter<<" "; } cout<<endl; //-------利用STL通用算法for_each()输出链表--------------- /* Print是个函数,实现对象的输出功能 */ cout<<"链表为:"; std::for_each(listintegers.begin(),listintegers.end(),Print); cout<<endl; //------利用STL通用算法find()推断链表中是否存在某元素---------- listiter=find(listintegers.begin(),listintegers.end(),6); if(listiter==listintegers.end()) { cout<<"6 is not in list"<<endl; } else { cout<<"6 is in list"<<endl; } //-------利用STL通用算法search()推断链表中是否存在某个序列------- list<int> targetlist; targetlist.push_front(2); targetlist.push_front(1); //定义该序列为12 listiter=search(listintegers.begin(),listintegers.end(),targetlist.begin(),targetlist.end()); if(listiter==listintegers.end()) { cout<<"序列12 is not in list"<<endl; } else { cout<<"序列12 is in list"<<endl; } //使用list的成员函数sort()对链表进行排序 cout<<"排序后,链表为:"; listintegers.sort(); for(listiter=listintegers.begin();listiter!=listintegers.end();listiter++) { cout<<*listiter<<" "; } cout<<endl; //使用list的成员函数remove()删除链表元素 listintegers.remove(8); cout<<"删除8后,链表为:"; for(listiter=listintegers.begin();listiter!=listintegers.end();listiter++) { cout<<*listiter<<" "; } cout<<endl; //----------使用list成员函数pop_front删除链首元素---------- listintegers.pop_front(); cout<<"删除链首元素后。链表为:"; for(listiter=listintegers.begin();listiter!=listintegers.end();listiter++) { cout<<*listiter<<" "; } cout<<endl; //----------使用list成员函数pop_back删除链尾元素---------- listintegers.pop_back(); cout<<"删除链尾元素后,链表为:"; for(listiter=listintegers.begin();listiter!=listintegers.end();listiter++) { cout<<*listiter<<" "; } cout<<endl; system("pause"); return 0; } 本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5380170.html,如需转载请自行联系原作者

相关文章
|
存储 缓存 C++
C++ 容器全面剖析:掌握 STL 的奥秘,从入门到高效编程
C++ 标准模板库(STL)提供了一组功能强大的容器类,用于存储和操作数据集合。不同的容器具有独特的特性和应用场景,因此选择合适的容器对于程序的性能和代码的可读性至关重要。对于刚接触 C++ 的开发者来说,了解这些容器的基础知识以及它们的特点是迈向高效编程的重要一步。本文将详细介绍 C++ 常用的容器,包括序列容器(`std::vector`、`std::array`、`std::list`、`std::deque`)、关联容器(`std::set`、`std::map`)和无序容器(`std::unordered_set`、`std::unordered_map`),全面解析它们的特点、用法
C++ 容器全面剖析:掌握 STL 的奥秘,从入门到高效编程
|
编译器 C语言 C++
【c++丨STL】list模拟实现(附源码)
本文介绍了如何模拟实现C++中的`list`容器。`list`底层采用双向带头循环链表结构,相较于`vector`和`string`更为复杂。文章首先回顾了`list`的基本结构和常用接口,然后详细讲解了节点、迭代器及容器的实现过程。 最终,通过这些步骤,我们成功模拟实现了`list`容器的功能。文章最后提供了完整的代码实现,并简要总结了实现过程中的关键点。 如果你对双向链表或`list`的底层实现感兴趣,建议先掌握相关基础知识后再阅读本文,以便更好地理解内容。
331 2
|
算法 C语言 C++
【c++丨STL】list的使用
本文介绍了STL容器`list`的使用方法及其主要功能。`list`是一种双向链表结构,适用于频繁的插入和删除操作。文章详细讲解了`list`的构造函数、析构函数、赋值重载、迭代器、容量接口、元素访问接口、增删查改操作以及一些特有的操作接口如`splice`、`remove_if`、`unique`、`merge`、`sort`和`reverse`。通过示例代码,读者可以更好地理解如何使用这些接口。最后,作者总结了`list`的特点和适用场景,并预告了后续关于`list`模拟实现的文章。
457 7
|
存储 搜索推荐 C++
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器2
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器
291 9
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器2
|
存储 C++ 容器
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器1
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器
313 5
|
存储 编译器 C++
【C++篇】揭开 C++ STL list 容器的神秘面纱:从底层设计到高效应用的全景解析(附源码)
【C++篇】揭开 C++ STL list 容器的神秘面纱:从底层设计到高效应用的全景解析(附源码)
302 2
|
安全 Java
java线程之List集合并发安全问题及解决方案
java线程之List集合并发安全问题及解决方案
1828 1
|
运维 关系型数据库 Java
PolarDB产品使用问题之使用List或Range分区表时,Java代码是否需要进行改动
PolarDB产品使用合集涵盖了从创建与管理、数据管理、性能优化与诊断、安全与合规到生态与集成、运维与支持等全方位的功能和服务,旨在帮助企业轻松构建高可用、高性能且易于管理的数据库环境,满足不同业务场景的需求。用户可以通过阿里云控制台、API、SDK等方式便捷地使用这些功能,实现数据库的高效运维与持续优化。
|
Java API Apache
怎么在在 Java 中对List进行分区
本文介绍了如何将列表拆分为给定大小的子列表。尽管标准Java集合API未直接支持此功能,但Guava和Apache Commons Collections提供了相关API。
541 1
|
存储 安全 Java
详解Java中集合的List接口实现的ArrayList方法 | Set接口实现的HashSet方法
详解Java中集合的List接口实现的ArrayList方法 | Set接口实现的HashSet方法
335 3