stdptr

ptr  时间:2021-02-18  阅读:()
ProgrammingLinkedListsMotivationA"List"isausefulstructuretoholdacollectionofdata.
Currently,weusearraysforlistsExamples:ListoftenstudentsmarksintstudentMarks[10];Listoftemperaturesforthelasttwoweeksdoubletemperature[14];MotivationlistusingstaticarrayintmyArray[10];Wehavetodecideinadvancethesizeofthearray(list)listusingdynamicarrayintn,*myArray;cin>>n;myArray=newint[n];Weallocateanarray(list)ofanyspecifiedsizewhiletheprogramisrunninglinked-list(dynamicsize)size=Thelistisdynamic.
Itcangrowandshrinktoanysize.
LinkedLists:BasicIdeaAlinkedlistisanorderedcollectionofdataEachelementofthelinkedlisthasSomedataAlinktothenextelementThelinkisusedtochainthedataExample:Alinkedlistofintegers:20457585DataLinkThelistcangrowandshrinkLinkedLists:BasicIdeas204575852045add(75),add(85)delete(85),delete(45),delete(20)75Originallinkedlistofintegers:Insertion:DeletionLinkedLists:Operations20457585204575852045758560oldvaluedeleteditem#includeusingnamespacestd;structNode{intdata;Node*next;};typedefNode*NodePtr;typedeftypedefallowsyoutomakenewshortcutstoexistingtypestypedefintWAH;WAHk;//sameas:intk;typedefint*WAHPTR;WAHPTRp;//sameas:int*p;typedefNode*NodePtr;NodePtrHead;//sameas:Node*Head;LinkedListStructureNode:Data+LinkDefinitionstructNode{intdata;//containsusefulinformationNode*next;//pointstonextelementorNULL};CreateaNodeNode*p;p=newNode;//pointstonewlyallocatedmemoryDeleteaNodedeletep;LinkedListStructuresAccessfieldsinanode(*p).
data;//accessthedatafield(*p).
next;//accessthepointerfieldOritcanbeaccessedthiswayp->data//accessthedatafieldp->next//accessthepointerfieldRepresentingandaccessingLinkedListsWedefineapointerNodePtrHead;thatpointstothefirstnodeofthelinkedlist.
WhenthelinkedlistisemptythenHeadisNULL.
20457585HeadPassingaLinkedListtoaFunctionWhenpassingalinkedlisttoafunctionitshouldsufficetopassthevalueofHead.
UsingthevalueofHeadthefunctioncanaccesstheentirelist.
Problem:Ifafunctionchangesthebeginningofalistbyinsertingordeletinganode,thenHeadwillnolongerpointtothebeginningofthelist.
Solution:WhenpassingHeadalwayspassitbyreference(orusingafunctiontoreturnanewpointervalue)20457585HeadManipulationofaUnsortedLinkedListStartthefirstnodefromscratchNodePtrnewPtr;newPtr=newNode;newPtr->data=20;newPtr->next=NULL;Head=newPtr;HeadnewPtr20HeadHead=NULL;InsertingaNodeattheBeginningnewPtr=newNode;newPtr->data=13;newPtr->next=Head;Head=newPtr;HeadnewPtr1320Keepgoing…HeadnewPtr50401320voidaddHead(NodePtr&Head,intnewdata){NodePtrnewPtr=newNode;newPtr->data=newdata;newPtr->next=Head;Head=newPtr;}Addinganelementtothehead:(todelete)DeletingtheHeadNodeNodePtrcur;cur=Head;Head=Head->next;deletecur;Headcur50401320voiddelHead(NodePtr&Head){if(Head!
=NULL){NodePtrcur=Head;Head=Head->next;deletecur;}}DisplayingaLinkedListcur=Head;cur=cur->next;2045Headcur2045HeadcurAlinkedlistisdisplayedbywalkingthroughitsnodesonebyone,anddisplayingtheirdatafields.
voidDisplayList(NodePtrHead){NodePtrcur;cur=Head;while(cur!
=NULL){coutdatanext;}}//returnthepointerofthenodehasdata=item//returnNULLifitemdoesnotexistNodePtrsearchNode(NodePtrHead,intitem){NodePtrCur=Head;NodePtrResult=NULL;while(Cur!
=NULL){if(Cur->data==item)Result=Cur;Cur=Cur->next;}returnResult;}SearchingforanodeOriginallinkedlistofintegers:Addtotheend(insertattheend):Moreoperation:addingtotheend504013205040132060LastelementThekeyishowtolocatethelastelementornodeofthelist!
voidaddEnd(NodePtr&Head,intnewdata){NodePtrlast=Head;NodePtrnewPtr=newNode;newPtr->data=newdata;newPtr->next=NULL;if(Head!
=NULL){//non-emptylistcasewhile(last->next!
=NULL)last=last->next;last->next=newPtr;}else//dealwiththecaseofemptylistHead=newPtr;}Addtotheend:Linknewobjecttolast->nextLinkanewobjecttoemptylistManipulationofaSortedLinkedListInsertingaNodeHeadcur20334575prev.
.
.
newPtrToinsertanewnodeintothelist1.
(a)Createanewnodeusing:NodePtrnewPtr=newnode;(b)Fillinthedatafieldcorrectly.
2.
Find"prev"and"cur"suchthatthenewnodeshouldbeinsertedbetween*prevand*cur.
3.
Connectthenewnodetothelistbyusing:(a)newPtr->next=cur;(b)prev->next=newPtr;FindingprevandcurSupposethatwewanttoinsertordeleteanodewithdatavaluenewValue.
Thenthefollowingcodesuccessfullyfindsprevandcur.
prev=NULL;cur=Head;while(cur!
=NULL&&newValue>cur->data){prev=cur;cur=cur->next;}//insertitemintolinkedlistinascendingordervoidinsertNode(NodePtr&Head,intitem){NodePtrNew,Cur,Pre;New=newNode;New->data=item;Pre=NULL;Cur=Head;while(Cur!
=NULL&&item>Cur->data){Pre=Cur;Cur=Cur->next;}if(Pre==NULL){//inserttoheadoflinkedlistNew->next=Head;Head=New;}else{Pre->next=New;New->next=Cur;}}(todelete)DeletingaNodeTodeleteanodefromthelist1.
Locatethenodetobedeleted(a)curpointstothenode.
(b)prevpointstoitspredecessor2.
Disconnectnodefromlistusing:prev->next=cur->next;3.
Returndeletednodetosystem:deletecur;Headcur20457585prev.
.
.
voiddeleteNode(NodePtr&Head,intitem){NodePtrprev,cur=Head;while(cur!
=NULL&&item>cur->data){prev=cur;cur=cur->next;}if(cur==NULL||cur->data!
=item){coutnext;elseprev->next=cur->next;deletecur;}Deleteanelementinasortedlinkedlist:

LOCVPS新上韩国KVM,全场8折,2G内存套餐月付44元起_网络传真服务器

LOCVPS(全球云)发布了新上韩国机房KVM架构主机信息,提供流量和带宽方式,适用全场8折优惠码,优惠码最低2G内存套餐月付仅44元起。这是一家成立较早的国人VPS服务商,目前提供洛杉矶MC、洛杉矶C3、和香港邦联、香港沙田电信、香港大埔、日本东京、日本大阪、新加坡、德国和荷兰等机房VPS主机,基于KVM或者XEN架构。下面分别列出几款韩国机房KVM主机配置信息。韩国KVM流量型套餐:KR-Pl...

HostSlim,双E5-2620v2/4x 1TB SATA大硬盘,荷兰服务器60美元月

hostslim美国独立日活动正在进行中,针对一款大硬盘荷兰专用服务器:双E5-2620v2/4x 1TB SATA硬盘,活动价60美元月。HostSlim荷兰服务器允许大人内容,不过只支持电汇、信用卡和比特币付款,商家支持7天内退款保证,有需要欧洲服务器的可以入手试试,记得注册的时候选择中国,这样不用交20%的税。hostslim怎么样?HostSlim是一家成立于2008年的荷兰托管服务器商,...

A400互联37.8元/季,香港节点cn2,cmi线路云服务器,1核/1G/10M/300G

A400互联怎么样?A400互联是一家成立于2020年的商家,A400互联是云服务器网(yuntue.com)首次发布的云主机商家。本次A400互联给大家带来的是,全新上线的香港节点,cmi+cn2线路,全场香港产品7折优惠,优惠码0711,A400互联,只为给你提供更快,更稳,更实惠的套餐,香港节点上线cn2+cmi线路云服务器,37.8元/季/1H/1G/10M/300G,云上日子,你我共享。...

ptr为你推荐
万维读者网《读者》要订购有网站吗?可以发外链的论坛发外链的论坛哪个比较好,哪个论坛能发外链,能发广告急求。。。。外网和内网什么是外网和内网?网易公开课怎么下载哪位高手指导一下,如何下载网易公开课啊?直播加速有没有软件使已经下载好了的视频播放加速,例如30分钟的视频15分钟或者20分钟播放完雅虎天盾高手进来看看我该怎么办 新装的ie8 内存使用率达到100%了淘宝网页显示不正常淘宝网页不能正常显示分词技术百度的中文分词原理是什么?与IK分词有区别吗?srv记录SRV记录的简介宽带接入服务器网络已连接,可无法连接到服务器为什么?网络已连接,可无法连接到服务
电信测速器 云网数据 香港新世界电讯 realvnc 商务主机 电子邮件服务器 秒杀汇 免费美国空间 免费智能解析 厦门电信 net空间 服务器防火墙 phpinfo ncp是什么 apachetomcat 隐士ddos 什么是云主机 qq登陆空间 qq空间排行榜 web服务器的配置 更多