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

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介:
#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,如需转载请自行联系原作者

相关文章
|
1天前
|
存储 安全 Java
Java容器类List、ArrayList、Vector及map、HashTable、HashMap
Java容器类List、ArrayList、Vector及map、HashTable、HashMap
|
2天前
|
调度 C++ 容器
【C++】手搓 list 容器
本文我们实现了STL库中重要的list 的模拟实现,其中最重要莫过于迭代器的封装类的书写,这是前所未有的操作(对于我来说,我是第一次使用这种结构)。通过list 的模拟实现也帮我们巩固了类与对象的知识,也强化了指针操作的思路。欢迎大家讨论分析。
12 1
|
5天前
|
存储 设计模式 算法
【C++/STL】stack和queue(容器适配器、优先队列、双端队列)
【C++/STL】stack和queue(容器适配器、优先队列、双端队列)
11 1
|
5天前
|
存储 编译器 C++
【C++/STL】list(常见接口、模拟实现、反向迭代器、)
【C++/STL】list(常见接口、模拟实现、反向迭代器、)
5 0
|
11天前
|
存储 算法 C++
详解C++中的STL(标准模板库)容器
【4月更文挑战第30天】C++ STL容器包括序列容器(如`vector`、`list`、`deque`、`forward_list`、`array`和`string`)、关联容器(如`set`、`multiset`、`map`和`multimap`)和容器适配器(如`stack`、`queue`和`priority_queue`)。它们为动态数组、链表、栈、队列、集合和映射等数据结构提供了高效实现。选择合适的容器类型可优化性能,满足不同编程需求。
|
13天前
|
C++ 容器
STL—map容器
STL—map容器
|
14天前
|
监控 Java Serverless
Serverless 应用引擎操作报错问题之有个容器一直重启如何解决
Serverless 应用引擎(SAE)是阿里云提供的Serverless PaaS平台,支持Spring Cloud、Dubbo、HSF等主流微服务框架,简化应用的部署、运维和弹性伸缩。在使用SAE过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
21 1
|
17天前
|
存储 算法 程序员
C++从入门到精通:2.2.1标准库与STL容器算法深度解析
C++从入门到精通:2.2.1标准库与STL容器算法深度解析
|
18天前
|
存储 缓存 编译器
【C++进阶(五)】STL大法--list模拟实现以及list和vector的对比
【C++进阶(五)】STL大法--list模拟实现以及list和vector的对比
|
1天前
|
NoSQL Redis Docker
Mac上轻松几步搞定Docker与Redis安装:从下载安装到容器运行实测全程指南
Mac上轻松几步搞定Docker与Redis安装:从下载安装到容器运行实测全程指南
10 0