双向循环链表list.docx
- 文档编号:25063824
- 上传时间:2023-06-04
- 格式:DOCX
- 页数:11
- 大小:45.31KB
双向循环链表list.docx
《双向循环链表list.docx》由会员分享,可在线阅读,更多相关《双向循环链表list.docx(11页珍藏版)》请在冰豆网上搜索。
双向循环链表list
list是双向循环链表,,每一个元素都知道前面一个元素和后面一个元素。
在STL中,list和vector一样,是两个常被使用的容器。
和vector不一样的是,list不支持对元素的任意存取。
list中提供的成员函数与vector类似,不过list提供对表首元素的操作push_front、pop_front,这是vector不具备的。
和vector另一点不同的是,list的迭代器不会存在失效的情况,他不像vector会保留备份空间,在超过容量额度时重新全部分配内存,导致迭代器失效;list没有备份空间的概念,出入一个元素就申请一个元素的空间,所以它的迭代器不会失效。
还是举《C++之vector》中的例子:
intdata[6]={3,5,7,9,2,4};
list
lidata.push_back(6);
...
list初始化时,申请的空间大小为6,存放下了data中的6个元素,当向lidata插入第7个元素“6”时,list申请新的节点单元,插入到list链表中,数据存放结构如图1所示:
图1list的存储结构
list每次增加一个元素,不存在重新申请内存的情况,它的成本是恒定的。
而vector每当增加关键元素的时候,都需要重新申请新的更大的内存空间,会调用元素的自身的复制构造函数,存在构造成本。
在销毁旧内存的时候,会调用析构函数,存在析构成本。
所以在存储复杂类型和大量元素的情况下,list比vector更有优势!
List是一个双向链表,双链表既可以向前又向后链接他的元素。
List将元素按顺序储存在链表中.与向量(vector)相比,它允许快速的插入和删除,但是随机访问却比较慢。
assign()给list赋值
back()返回最后一个元素
begin()返回指向第一个元素的迭代器
clear()删除所有元素
empty()如果list是空的则返回true
end()返回末尾的迭代器
erase()删除一个元素
front()返回第一个元素
get_allocator()返回list的配置器
insert()插入一个元素到list中
max_size()返回list能容纳的最大元素数量
merge()合并两个list
pop_back()删除最后一个元素
pop_front()删除第一个元素
push_back()在list的末尾添加一个元素
push_front()在list的头部添加一个元素
rbegin()返回指向第一个元素的逆向迭代器
remove()从list删除元素
remove_if()按指定条件删除元素
rend()指向list末尾的逆向迭代器
resize()改变list的大小
reverse()把list的元素倒转
size()返回list中的元素个数
sort()给list排序
splice()合并两个list
swap()交换两个list
unique()删除list中重复的元素
List使用实例1
#include
#include
#include
#include
usingnamespacestd;
//创建一个list容器的实例LISTINT
typedeflist
//创建一个list容器的实例LISTCHAR
typedeflist
intmain(intargc,char*argv[])
{
//--------------------------
//用list容器处理整型数据
//--------------------------
//用LISTINT创建一个名为listOne的list对象
LISTINTlistOne;
//声明i为迭代器
LISTINT:
:
iteratori;
//从前面向listOne容器中添加数据
listOne.push_front
(2);
listOne.push_front
(1);
//从后面向listOne容器中添加数据
listOne.push_back(3);
listOne.push_back(4);
//从前向后显示listOne中的数据
cout<<"listOne.begin()---listOne.end():
"< for(i=listOne.begin();i! =listOne.end();++i) cout<<*i<<""; cout< //从后向后显示listOne中的数据 LISTINT: : reverse_iteratorir; cout<<"listOne.rbegin()---listOne.rend(): "< for(ir=listOne.rbegin();ir! =listOne.rend();ir++){ cout<<*ir<<""; } cout< //使用STL的accumulate(累加)算法 intresult=accumulate(listOne.begin(),listOne.end(),0); cout<<"Sum="< cout<<"------------------"< //-------------------------- //用list容器处理字符型数据 //-------------------------- //用LISTCHAR创建一个名为listOne的list对象 LISTCHARlistTwo; //声明i为迭代器 LISTCHAR: : iteratorj; //从前面向listTwo容器中添加数据 listTwo.push_front('A'); listTwo.push_front('B'); //从后面向listTwo容器中添加数据 listTwo.push_back('x'); listTwo.push_back('y'); //从前向后显示listTwo中的数据 cout<<"listTwo.begin()---listTwo.end(): "< for(j=listTwo.begin();j! =listTwo.end();++j) cout< cout< //使用STL的max_element算法求listTwo中的最大元素并显示 j=max_element(listTwo.begin(),listTwo.end()); cout<<"ThemaximumelementinlistTwois: "< return0; } List使用实例2 list: Linkedlistofvariables,structorobjects.Insert/removeanywhere. Twoexamplesaregiven: 1.ThefirstSTLexampleisfordatatype int 2.Thesecondforalistof class instances. Theyareusedtoshowasimpleexampleandamorecomplexrealworldapplication. 1.LetsstartwithasimpleexampleofaprogramusingSTLforalinkedlist: //Simpleexampleusestypeint #include #include usingnamespacestd; intmain() { list L.push_back(0); //Insertanewelementattheend L.push_front(0); //Insertanewelementatthebeginning L.insert(++L.begin(),2); //Insert"2"beforepositionoffirstargument //(Placebeforesecondargument) L.push_back(5); L.push_back(6); list : iteratori; for(i=L.begin();i! =L.end();++i)cout<<*i<<""; cout< return0; } Compile: g++example1.cpp Run: ./a.out Output: 02056 2.TheSTLtutorialsandtextsseemtogivesimpleexampleswhichdonotapplytotherealworld.Thefollowingexampleisforadoublylinkedlist.Sinceweareusingaclassandwearenotusingdefinedbuilt-inC++typeswehaveincludedthefollowing: ∙Tomakethisexamplemorecomplete,acopyconstructorhasbeenincludedalthoughthecompilerwillgenerateamember-wiseoneautomaticallyifneeded.Thishasthesamefunctionalityastheassignmentoperator(=). ∙Theassignment(=)operatormustbespecifiedsothatsortroutinescanassignanewordertothemembersofthelist. ∙The"lessthan"(<)operatormustbespecifiedsothatsortroutinescandetermineifoneclassinstanceis"lessthan"another. ∙The"equalsto"(==)operatormustbespecifiedsothatsortroutinescandetermineifoneclassinstanceis"equalsto"another. //StandardTemplateLibraryexampleusingaclass. #include #include usingnamespacestd; //TheListSTLtemplaterequiresoverloadingoperators=,==and<. //vc2005调试没有错(红色字体部分可去掉)、可用vc6.0却报错了“'operator<<'isambiguous”(vc6.0的加上红色字体部分) classAAA; ostream&operator<<(ostream&output,constAAA&aaa); classAAA { friendostream&operator<<(ostream&,constAAA&); public: intx; inty; floatz; AAA(); AAA(constAAA&); ~AAA(){}; AAA&operator=(constAAA&rhs); intoperator==(constAAA&rhs)const; intoperator<(constAAA&rhs)const; }; AAA: : AAA() //Constructor { x=0; y=0; z=0; } AAA: : AAA(constAAA©in) //Copyconstructortohandlepassbyvalue. { x=copyin.x; y=copyin.y; z=copyin.z; } ostream&operator<<(ostream&output,constAAA&aaa) { output< returnoutput; } AAA&AAA: : operator=(constAAA&rhs) { this->x=rhs.x; this->y=rhs.y; this->z=rhs.z; return*this; } intAAA: : operator==(constAAA&rhs)const { if(this->x! =rhs.x)return0; if(this->y! =rhs.y)return0; if(this->z! =rhs.z)return0; return1; } //Thisfunctionisrequiredforbuilt-inSTLlistfunctionslikesort intAAA: : operator<(constAAA&rhs)const { if(this->x==rhs.x&&this->y==rhs.y&&this->z if(this->x==rhs.x&&this->y if(this->x return0; } intmain() { list AAAAblob; Ablob.x=7; Ablob.y=2; Ablob.z=4.2355; L.push_back(Ablob); //Insertanewelementattheend Ablob.x=5; L.push_back(Ablob); //Objectpassedbyvalue.Usesdefaultmember-wise //copyconstructor Ablob.z=3.2355; L.push_back(Ablob); Ablob.x=3; Ablob.y=7; Ablob.z=7.2355; L.push_back(Ablob); list : iteratori; for(i=L.begin();i! =L.end();++i)cout<<(*i).x<<"";//printmember cout< for(i=L.begin();i! =L.end();++i)cout<<*i<<"";//printwithoverloadedoperator cout< cout<<"Sorted: "< L.sort(); for(i=L.begin();i! =L.end();++i)cout<<*i<<"";//printwithoverloadedoperator cout< return0; } Output: 7553 724.2355 524.2355 523.2355 377.2355 Sorted: 377.2355 523.2355 524.2355 724.2355
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 双向 循环 list