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:

hostodo:2021美国独立日7折优惠促销NVMe硬盘的VPS,低至$13/年,还送DirectAdmin授权

7月4日是美国独立日,大致就是国庆节的意思吧。hostodo今年提前搞了个VPS大促销活动,4款便宜VPS,相当于7折,续费不涨价,本次促销不定时,不知道有多少货,卖完为止。VPS基于KVM虚拟,NVMe阵列,1Gbps带宽,自带一个IPv4+/64 IPv6,solusvm管理,送收费版DirectAdmin授权,VPS在用就有效! 官方网站:https://www.hostodo.com ...

A400:36元/季,16.8/月kvm架构,线路优质,延迟低

A400互联是一家成立于2020年的商家,主要推行洛杉矶服务器采用kvm架构,线路优质,延迟低,稳定性高!全场产品对标腾讯云轻量,服务器线路有有美国洛杉矶cn2_gia、香港cn2+cmi,目前推行的vps服务器均为精心挑选的优质线路机房,A400互联推出了夏季优惠洛杉矶5折、香港7折促销活动,质量可靠,价格实惠!二:优惠码洛杉矶五折优惠码:20210620香港cn2七折优惠码:0710三、优惠方...

俄罗斯vps主机推荐,怎么样俄罗斯vps俄罗斯vps速度怎么样?

俄罗斯vps速度怎么样?俄罗斯vps云主机节点是欧洲十大节点之一,地处俄罗斯首都莫斯科,网络带宽辐射周边欧洲大陆,10G专线连通德国法兰克福、法国巴黎、意大利米兰等,向外连接全球。俄罗斯vps云主机速度快吗、延迟多少?由于俄罗斯数据中心出口带宽充足,俄罗斯vps云主机到全球各地的延迟、速度相对来说都不错。今天,云服务器网(yuntue.com)小编介绍一下俄罗斯vps速度及俄罗斯vps主机推荐!俄...

ptr为你推荐
cornerradius怎么用代码写一个圆角矩形?51自学网站推荐一个好一点的自学网站,关于网站建设的。不兼容WIN7 64位系统与某些软件不兼容怎么办?网店推广网站可以介绍几个可以做店铺推广的网站吗?腾讯文章怎样才能在手机腾讯网上发表文章?linux虚拟机怎么样在Linux下安装虚拟机安全漏洞如何发现系统安全漏洞网络广告投放怎样在网络上进行广告的投放?网络虚拟机虚拟机的网络怎么弄?服务器连接异常主服务器连接异常
新网域名 江西服务器租用 vps代理 怎样申请域名 漂亮qq空间 justhost adman 樊云 2014年感恩节 秒杀预告 cloudlink 创建邮箱 google台湾 德讯 lamp的音标 杭州电信宽带优惠 七牛云存储 cdn服务 hostease 双11促销 更多