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:

香港ceranetworks(69元/月) 2核2G 50G硬盘 20M 50M 100M 不限流量

香港ceranetworks提速啦是成立于2012年的十分老牌的一个商家这次给大家评测的是 香港ceranetworks 8核16G 100M 这款产品 提速啦老板真的是豪气每次都给高配我测试 不像别的商家每次就给1核1G,废话不多说开始跑脚本。香港ceranetworks 2核2G 50G硬盘20M 69元/月30M 99元/月50M 219元/月100M 519元/月香港ceranetwork...

艾云年付125元圣何塞GTT,洛杉矶vps年付85元

艾云怎么样?艾云是一家去年年底成立的国人主机商家,商家主要销售基于KVM虚拟架构的VPS服务,机房目前有美国洛杉矶、圣何塞和英国伦敦,目前商家推出了一些年付特价套餐,性价比非常高,洛杉矶套餐低至85元每年,给500M带宽,可解奈飞,另外圣何塞也有特价机器;1核/1G/20G SSD/3T/2.5Gbps,有需要的朋友以入手。点击进入:艾云官方网站艾云vps促销套餐:KVM虚拟架构,自带20G的防御...

HostYun(25元)俄罗斯CN2广播IP地址

从介绍看啊,新增的HostYun 俄罗斯机房采用的是双向CN2线路,其他的像香港和日本机房,均为国内直连线路,访问质量不错。HostYun商家通用九折优惠码:HostYun内存CPUSSD流量带宽价格(原价)购买地址1G1核10G300G/月200M28元/月购买链接1G1核10G500G/月200M38元/月购买链接1G1核20G900G/月200M68元/月购买链接2G1核30G1500G/月...

ptr为你推荐
暴风影音怎么截图暴风影音3 如何截图暴风影音怎么截图怎么截取暴风影音图片flash导航条flash导航条swf格式的要怎么编辑pwPW考试是指什么1433端口路由器1433端口怎么开启网站运营网络运营主管的主要工作职责是什么?百度手写百度输入法切换手写 百度汉王手写输入法ps抠图技巧如何使用PS抠图开机滚动条怎么减少开机滚动条?人人逛街人人都喜欢逛街吗
云主机租用 openv hostmonster cpanel主机 googleapps 美国便宜货网站 免费ddos防火墙 网通代理服务器 魔兽世界台湾服务器 阿里云浏览器 腾讯云分析 共享主机 免费吧 服务器托管什么意思 双12 河南移动梦网 韩国代理ip 英雄联盟台服官网 国内域名 百度云空间 更多