cb02a_c++_数据结构_顺序容器_STL_list类_双向链表
/*cb02a_c++_数据结构_顺序容器_STL_list类_双向链表
实例化std::list对象
在list开头插入元素
在list末尾插入元素
在list中间插入元素,插入时间恒定,非常快。数组:中间插入慢。
删除list中的元素
对list中元素进行反转和排序
通过指针指向下一个节点
//链表不是数组,没有下标。只能使用迭代器
*/
/*cb02a_c++_数据结构_顺序容器_STL_list类_双向链表
实例化std::list对象
在list开头插入元素
在list末尾插入元素
在list中间插入元素,插入时间恒定,非常快。数组:中间插入慢。
删除list中的元素
对list中元素进行反转和排序 通过指针指向下一个节点
//链表不是数组,没有下标。只能使用迭代器
*/ #include <iostream>
#include <list> using namespace std; void PrintListContents(const list<int>& listInput); int main()
{
list <int> a;//list也是一个模板类,a就是双向链表
a.push_front();//链表前端添加数据
a.push_front();
a.push_front();
a.push_front();
a.push_back();//链表后端添加数据 //a.insert(a.begin(), 10);//在开头的前面插入10。 a.begin()就是迭代器 list<int> b;
b.push_back();
b.push_back();
b.push_back();
b.push_back();
b.push_back(); //链表不是数组,没有下标
std::list<int>::iterator iter;//迭代器就是指针 iter = a.begin();
a.insert(iter, );//在开头的前面插入11。
a.insert(a.end(), , );//在后端插入3个30,a.end()就是迭代器 ++iter;
a.insert(iter, );//在开头的下一个位置插入11.++iter指针移动了位置
//a.insert(a.end(), b.begin(), b.end());//把list b的数据全部插入到list a的末尾 a.insert(a.end(),++b.begin(),--b.end());//b的第二个位置数据到 b结尾倒数一个数。一起插入 cout << "show list a data..." << endl;
PrintListContents(a); /*for (iter = a.begin(); iter != a.end(); ++iter)
{
cout << *iter << endl;
}*/
cout << "show list b data" << endl;
PrintListContents(b); return ;
}
void PrintListContents(const list<int>& listInput)
{
std::list<int>::const_iterator iter;
for (iter = listInput.begin(); iter != listInput.end(); ++iter)
cout << *iter << endl;
}
/*cb02b_c++_ */ /*cb02a_c++_数据结构_顺序容器_STL_list类_双向链表
实例化std::list对象
在list开头插入元素
在list末尾插入元素
在list中间插入元素,插入时间恒定,非常快。数组:中间插入慢。
删除list中的元素
对list中元素进行反转和排序 通过指针指向下一个节点
//链表不是数组,没有下标。只能使用迭代器
*/ #include <iostream>
#include <list> using namespace std; void PrintListContents(const list<int>& listInput); int main()
{
std::list<int> a;
a.push_front();
a.push_front(); list<int>::iterator iElementValueTwo;
iElementValueTwo = a.insert(a.begin(),);//insert返回一个迭代器,指向2.
a.push_front();
a.push_front(); PrintListContents(a);// //iElementValueTwo--迭代器,指向2.
a.erase(iElementValueTwo);//删除一个元素:2
cout << "把2删除后显示:" << endl;
PrintListContents(a);// //a.erase(a.begin(), iElementValueTwo);//从开始到2,2不包括。删除。
a.erase(iElementValueTwo, a.end());//从2开始直到最后的所有数据,删除。 return ;
}
void PrintListContents(const list<int>& listInput)
{
std::list<int>::const_iterator iter;
for (iter = listInput.begin(); iter != listInput.end(); ++iter)
cout << *iter << endl;
}
/*cb02c_c++_反转和排序 */ /*cb02a_c++_数据结构_顺序容器_STL_list类_双向链表
实例化std::list对象
在list开头插入元素
在list末尾插入元素
在list中间插入元素,插入时间恒定,非常快。数组:中间插入慢。
删除list中的元素
对list中元素进行反转和排序 通过指针指向下一个节点
//链表不是数组,没有下标。只能使用迭代器
*/ #include <iostream>
#include <list> using namespace std; void PrintListContents(const list<int>& listInput); int main()
{
std::list<int> a;
a.push_front();
a.push_front();
a.push_front();
a.push_front();
a.push_front(); PrintListContents(a); //翻转
cout << "数据翻转: " << endl;
a.reverse();
PrintListContents(a); cout << "链表数据排序:" << endl;
a.sort();
PrintListContents(a); return ;
}
void PrintListContents(const list<int>& listInput)
{
std::list<int>::const_iterator iter;
for (iter = listInput.begin(); iter != listInput.end(); ++iter)
cout << *iter << endl;
}
cb02a_c++_数据结构_顺序容器_STL_list类_双向链表的更多相关文章
- cb01a_c++_数据结构_顺序容器_STL_deque类
/*cb01a_c++_数据结构_顺序容器_STL_deque类deque是一个动态数组,比vector更加灵活.两者都属于动态数组deque与vector非常类似deque可以在数组开头和末尾插入和 ...
- cb16a_c++_顺序容器的选用_排序_二分查找
/*cb16a_c++_顺序容器的选用_排序_二分查找顺序容器: 1.vector的优点与缺点 vector优点:排序利用下标,快速排序,做二分查找非常快 2.list的优点与缺点 list优点:插入 ...
- cb09a_c++_顺序容器的操作2-在顺序容器中添加元素_插入数据
cb09a_c++_顺序容器的操作2在顺序容器中添加元素vector不能向前插入数据,list可以用insertc.push_back(t);c.push_front(t);c.insert(p,t) ...
- cb03a_c++_数据结构_顺序容器_STL_stack
/*cb03a_c++_数据结构_顺序容器_STL_stack堆栈:LIFO--Last In First Out后进先出,用于系统程序设计自适应容器(容器适配器),不是独立的容器,是一个适配器栈适配 ...
- cb06a_c++_顺序容器的定义
/*cb06a_c++_顺序容器的定义顺序容器:vector,数组,尾端操作数据,快速随机访问list 链表,快速插入数据deque数组,双端-首尾操作数据,方便两端的数据访问 顺序容器适配器:sta ...
- cb22a_c++_标准模板库_STL_map_multimap红黑树(数据结构)关联容器
cb22a_c++_标准模板库_STL_map_multimap红黑树(数据结构)关联容器map(映射,key不能重复,一对一对的,value_type(1, "one")),mu ...
- cb14a_c++_顺序容器的操作7_赋值与交换(swap)_vector转list
cb14a_c++_顺序容器的操作7_赋值与交换(swap) vector数据赋值给list, slist.assign(svec.begin(), svec.end());//这样可以转 svec- ...
- ca13a_c++_顺序容器的操作6删除元素
/*ca13a_c++_顺序容器的操作6删除元素c.erase(p) //删除迭代器p指向的位置c.erase(b,e) //删除b to e之间的数据,迭代器b包括,e不包括c.clear()//删 ...
- cb11a_c++_顺序容器的操作4_容器大小操作_resize-max_size
cb11a_c++_顺序容器的操作4 2 容器大小的操作 3 c.size() 容器当前的个数 4 c.max_size(),容器最大存储量 5 c.empty() 是否为空 6 c.resize(n ...
随机推荐
- eatwhatApp开发实战(七)
之前我们为app添加了读取本地数据的功能和删除的功能.本次我们来将listview上item项的触控修改为item项上单一控件的触控事件.用item项上的button来实现删除数据. 先上布局: &l ...
- [软件版本贴]SD.TEAM软件集
手机轰炸机-版本:v1.0.下载地址:http://pan.baidu.com/1.zip! 营销软件盒-版本:v1.0.下载地址:http://pan.baidu.com/2.zip! 不加群提取群 ...
- [工具推荐]003.Tortoisegit使用教程(补充)
前文介绍: 在前文<[工具推荐]003.Tortoisegit使用教程>中详细介绍了如何使用Tortoisegit的使用,但是大家使用后反映一点,就是每次操作都需要输入账号名和 ...
- 【转】shell的反引号、单引号、双引号的作用
Linux Shell中有三种引号,分别为双引号(" ").单引号(' ')以及反引号(` `). 其中双引号对字符串中出现的$.''.`和\进行替换:单引号不进行替换,将字符串中 ...
- JAVA WEB EL表达式注入
看猪猪侠以前的洞,顺便总结下: 一.EL表达式简介 EL 全名为Expression Language.EL主要作用: 1.获取数据 EL表达式主要用于替换JSP页面中的脚本表达式,以从各种类型的we ...
- leetcode56之合并区间
题目描述: 给出一个区间的集合,请合并所有重叠的区间. 示例: 输入: [[1,3],[2,6],[8,10],[15,18]]输出: [[1,6],[8,10],[15,18]]解释: 区间 [1, ...
- Rocket - diplomacy - IdRange
https://mp.weixin.qq.com/s/qqL2XWqAhVcnGSxs6kxhLg 介绍IdRange的实现. 1. 基本定义 A non-empty half- ...
- 【Flume】安装与测试
1.下载安装包http://archive.apache.org/dist/flume/ 2.解压命令tar -zxvf 压缩包 -C 路径 3.配置环境变量 export FLUME_HOME=/o ...
- Java实现 LeetCode 725 分隔链表(暴力)
725. 分隔链表 给定一个头结点为 root 的链表, 编写一个函数以将链表分隔为 k 个连续的部分. 每部分的长度应该尽可能的相等: 任意两部分的长度差距不能超过 1,也就是说可能有些部分为 nu ...
- Java实现洛谷 P1062 数列
P1062 数列 import java.util.ArrayList; import java.util.Scanner; public class Main { public static voi ...