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:

imidc:$88/月,e3-1230/16G内存/512gSSD/30M直连带宽/13个IPv4日本多IP

imidc对日本独立服务器在搞特别促销,原价159美元的机器现在只需要88美元,而且给13个独立IPv4,30Mbps直连带宽,不限制流量。注意,本次促销只有一个链接,有2个不同的优惠码,你用不同的优惠码就对应着不同的配置,价格也不一样。88美元的机器,下单后默认不管就给512G SSD,要指定用HDD那就发工单,如果需要多加一个/28(13个)IPv4,每个月32美元...官方网站:https:...

香港物理服务器 E5-2660v2 16G 500GSSD 增送20G防御 688/月 华纳云

#年终感恩活动#华纳云海外物理机688元/月,续费同价,50M CN2 GIA/100M国际大带宽可选,超800G 防御,不限流华纳云成立于2015年,隶属于香港联合通讯国际有限公司。拥有香港政府颁发的商业登记证明,作为APNIC 和 ARIN 会员单位,现有香港、美国等多个地区数据中心资源,百G丰富带宽接入,坚持为海内外用户提供自研顶级硬件防火墙服务,支持T B级超大防护带宽,单IP防护最大可达...

IMIDC(rainbow cloud):香港/台湾/日本/莫斯科独立服务器特价,闪购大促销,最低30usd/月起

imidc怎么样?imidc彩虹网路,rainbow cloud知名服务器提供商。自营多地区数据中心,是 Apnic RIPE Afrinic Arin 认证服务商。拥有丰富的网路资源。 在2021年 6.18 开启了输血大促销,促销区域包括 香港 台湾 日本 莫斯科 等地促销机型为 E3係,参与促销地区有 香港 日本 台湾 莫斯科 等地, 限量50台,售罄为止,先到先得。所有服务器配置 CPU ...

ptr为你推荐
博客外链外链都要怎么做?博客外链有没有效果?网站运营刚创业的网站运营怎么做?唱吧电脑版官方下载唱吧有电脑版吗网店推广网站开网店如何做推广?彩信中心移动的短信中心号码是多少腾讯文章为什么最近腾讯网的文章评论都看不到硬盘人移动硬盘的优缺点iphone6上市时间苹果6什么时候在中国大陆上市怎么上传音乐怎么上传音乐到网上服务器连接异常服务器连接异常,即将退出,请重新进入游戏.怎么回事
com域名 如何申请域名 vps租用 什么是域名地址 科迈动态域名 godaddy续费优惠码 360抢票助手 500m空间 嘉洲服务器 52测评网 域名和空间 tna官网 常州联通宽带 免费mysql数据库 美国独立日 联通网站 shuang12 中国电信网络测速 湖南idc ledlamp 更多