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:

提速啦:美国多IP站群云服务器 8核8G 10M带宽 7IP 88元/月

提速啦(www.tisula.com)是赣州王成璟网络科技有限公司旗下云服务器品牌,目前拥有在籍员工40人左右,社保在籍员工30人+,是正规的国内拥有IDC ICP ISP CDN 云牌照资质商家,2018-2021年连续4年获得CTG机房顶级金牌代理商荣誉 2021年赣州市于都县创业大赛三等奖,2020年于都电子商务示范企业,2021年于都县电子商务融合推广大使。资源优势介绍:Ceranetwo...

MechanicWeb免费DirectAdmin/异地备份

MechanicWeb怎么样?MechanicWeb好不好?MechanicWeb成立于2008年,目前在美国洛杉矶、凤凰城、达拉斯、迈阿密、北卡、纽约、英国、卢森堡、德国、加拿大、新加坡有11个数据中心,主营全托管型虚拟主机、VPS主机、半专用服务器和独立服务器业务。MechanicWeb只做高端的托管vps,这次MechanicWeb上新Xeon W-1290P处理器套餐,基准3.7GHz最高...

TmhHost 全场八折优惠且充值返10% 多款CN2线路

TmhHost 商家是一家成立于2019年的国人主机品牌。目前主营的是美国VPS以及美国、香港、韩国、菲律宾的独立服务器等,其中VPS业务涵盖香港CN2、香港NTT、美国CN2回程高防、美国CN2 GIA、日本软银、韩国cn2等,均为亚太中国直连优质线路,TmhHost提供全中文界面,支持支付宝付款。 TmhHost黑五优惠活动发布了,全场云服务器、独立服务器提供8折,另有充值返现、特价服务器促销...

ptr为你推荐
yy频道中心YY频道管理中心怎么登录?iphone5解锁捡了个苹果5怎么解锁如何免费开通黄钻如何免费开通黄钻手机区号手机号码数码资源网哪个网站可以直接在线做照片?功能要齐全的`安卓应用平台哪个手机应用平台的软件比较正版,安全?开机滚动条电脑开机滚动条要走好几次xv播放器下载除了迅雷看看播放器还有什么播放器支持xv格式的视频?奇虎论坛奇虎是中国的吗?ejb开发什么是ejb?
域名代理 美国虚拟主机推荐 justhost pccw cpanel omnis 便宜建站 牛人与腾讯客服对话 免费个人网站申请 好看qq空间 阿里校园 香港亚马逊 cdn服务 学生机 碳云 .htaccess web是什么意思 asp介绍 低价 丹弗润滑油 更多