您当前的位置: 首页 >  容器

txwtech

暂无认证

  • 4浏览

    0关注

    813博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

cb02a_c++_数据结构_顺序容器_STL_list类_双向链表

txwtech 发布时间:2020-02-13 17:07:28 ,浏览量:4

/*cb02a_c++_数据结构_顺序容器_STL_list类_双向链表 实例化std::list对象 在list开头插入元素 在list末尾插入元素 在list中间插入元素,插入时间恒定,非常快。数组:中间插入慢。 删除list中的元素 对list中元素进行反转和排序

通过指针指向下一个节点 //链表不是数组,没有下标。只能使用迭代器

welcome to disscuss

txwtech@163.com */

/*cb02a_c++_数据结构_顺序容器_STL_list类_双向链表
实例化std::list对象
在list开头插入元素
在list末尾插入元素
在list中间插入元素,插入时间恒定,非常快。数组:中间插入慢。
删除list中的元素
对list中元素进行反转和排序

通过指针指向下一个节点
//链表不是数组,没有下标。只能使用迭代器
*/

#include 
#include 

using namespace std;

void PrintListContents(const list& listInput);

int main()
{
	list  a;//list也是一个模板类,a就是双向链表
	a.push_front(10);//链表前端添加数据
	a.push_front(9);
	a.push_front(8);
	a.push_front(7);
	a.push_back(11);//链表后端添加数据

	//a.insert(a.begin(), 10);//在开头的前面插入10。 a.begin()就是迭代器
	

	
	list b;
	b.push_back(100);
	b.push_back(200);
	b.push_back(300);
	b.push_back(400);
	b.push_back(500);

	//链表不是数组,没有下标
	std::list::iterator iter;//迭代器就是指针

	iter = a.begin();
	a.insert(iter, 11);//在开头的前面插入11。
	a.insert(a.end(), 3, 30);//在后端插入3个30,a.end()就是迭代器

	++iter;
	a.insert(iter, 11);//在开头的下一个位置插入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             
关注
打赏
1665060526
查看更多评论
0.2647s