浙江工业大学数据结构第三章讲课文档_第1页
浙江工业大学数据结构第三章讲课文档_第2页
浙江工业大学数据结构第三章讲课文档_第3页
浙江工业大学数据结构第三章讲课文档_第4页
浙江工业大学数据结构第三章讲课文档_第5页
已阅读5页,还剩30页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

浙江工业大学数据结构第三章第1页,共35页。2MainIndexContentsLinkedListNodesEachNodeislikeapieceofachainToinsertanewlink,breakthechainatthedesiredlocationandsimplyreconnectatbothendsofthenewpiece.2第2页,共35页。3MainIndexContentsLinkedListNodesRemovalislikeInsertioninreverse.3第3页,共35页。4MainIndexContentsNodeCompositionAnindividualNodeiscomposedoftwoparts,aDatafieldcontainingthedatastoredbythenode,andaPointerfieldthatmarkstheaddressofthenextNodeinthelist.4第4页,共35页。5MainIndexContentsNode结点设计Template<typenameT>classnode{public:TnodeValue;//dataheldbythenodenode<T>*next;//nextnodeinthelistnode():next(NULL){}node(constT&item,node<T>*nextNode=NULL):nodeValue(item),next(nextNode){}};5第5页,共35页。AbstractModelofaListObjectdatanextdatanextdatanextdatanullfirst20xbfffaa28first0xbfffaa200xbfffaa280xbfffaa600xbfffaa8040xbfffaa6060xbfffaa808Nullfirst是指向node类型的指针:node<int>*first;first=newnode<int>(2,NULL);

0xbfffaa206第6页,共35页。7MainIndexContents1.如何生成链表node<int>*nodeptr1,*nodeptr2;nodeptr1=newnode<int>(1,null);nodeptr2=newnode<int>(2,null);思考:如何连接两个结点?nodeptr1->next=nodeptr2;nodeptr3=newnode<int>(3,null);nodeptr2->next=nodeptr3;7第7页,共35页。8MainIndexContents将1,2,3,4,5顺序插入链表(正向插入)node<int>*nodeptr1,*nodeptr2,*front;for(inti=1;i<6;i++){ nodeptr1=newnode<int>(i,NULL); nodeptr1->next=?}nodeptr1=newnode<int>(1,NULL);Front=nodeptr1;for(inti=2;i<6;i++){ nodeptr2=newnode<int>(i,NULL); nodeptr1->next=nodeptr2; nodeptr1=nodeptr1->next;}2.CreatingaLinkedList8第8页,共35页。9MainIndexContents2.CreatingaLinkedList将1,2,3,4,5顺序插入链表(反向插入)node<int>*front=NULL,*newNode;inti;for(i=1;i<=5;i++){ front=newnode<int>(i,front);}9第9页,共35页。10MainIndexContents3.TraverseaLinkedList1.如何全部遍历while(front!=Null){cout<<front->nodeValue;front=front->next;}2.如何访问尾部元素while(front->next!=NULL){ front=front->next;}10第10页,共35页。11MainIndexContents4.InertanodeatfrontorbackofaLinkedList1.在表头插入结点node<int>*p;p=newnode<int>(6,front);front=p;2.在表尾插入结点node<int>*q=front;while(q->next!=NULL){ q=q->next;}p=newnode<int>(6,NULL);q->next=p;11第11页,共35页。12MainIndexContents5.LinkedListwithbackpointernode<int>*front=Null,*newNode,*back;inti=0;while(i<5){ if(i==0){ front=newnode<int>(i,NULL); back=front; } else{ //back指针总是指向当前链表的最后一个结点

newNode=newnode<int>(i,NULL); back->next=newNode; back=back->next; } i++;}//创建链表的不同编程风格,使用back指针12第12页,共35页。13MainIndexContentsTheinitiallistisempty:front=NULL,back=NULL.

5.1aLinkedListwithbackpointer13第13页,共35页。14MainIndexContents5.2aLinkedListwithbackpointer14第14页,共35页。15MainIndexContents5.3InsertinganodeatbackwithabackpointerNode<T>*front,*back,*newNode;newNode=newnode<T>(item);If(front!=Null) back->next=newNode;else front=newNode;back=newNode;15第15页,共35页。16MainIndexContents5.4.InsertinganodeatfrontwithabackpointerNode<T>*front,*back,*newNode;newNode=newnode<T>(item,front);If(front==Null) back=newNode;front=newNode;16第16页,共35页。17MainIndexContents6.DeletingFromtheFrontofaLinkedListfrontfront//front=NULLDeletingfrontofa1-nodelistDeletingfrontofamulti-nodelist//front=front->next17第17页,共35页。18MainIndexContents

6.1DeletinganodeatfrontNode<T>*front,*p;If(front!=Null){ p=front; front=front->next; deletep:}18第18页,共35页。19MainIndexContents

6.2DeletinganodeatfrontwithabackpointerNode<T>*front,*back,*p;If(front!=Null){ p=front; front=front->next; if(front==Null) back=Null; deletep:}19第19页,共35页。20MainIndexContents

6.3DeletinganodeatbackNode<T>*front,*p;node<int>*p=front;if(p==NULL)exit(1); //空链表if(p->next==NULL)deletep; //只有一个结点while(p->next->next!=NULL){ p=p->next;//遍历到倒数第二个}q=p->next;p->next=NULL;deleteq;20第20页,共35页。21MainIndexContents7.InsertinganddeletinganodeInsert:newNode->next=curr;Prev->next=newNode;Delete:node<T>*curr,*prev;prev->next=curr->next;deletecurr;21第21页,共35页。22MainIndexContents8.DeletingaLinkedListnode<T>*p;while(front!=NULL){ p=front; front=front->next; deletep;}22第22页,共35页。23MainIndexContents单链表基本操作小结1.如何创建链表(正向、反向创建)node<int>*front=Null,*newNode,*back;

inti=0;while(i<5){ if(i==0){ front=newnode<int>(i,NULL); back=front; } else{ //back指针总是指向当前链表的最后一个结点

newNode=newnode<int>(i,NULL); back->next=newNode; back=back->next; } i++;}for(i=1;i<=5;i++){ front=newnode<int>(i,front);}23第23页,共35页。24MainIndexContents单链表基本操作2.如何在链表表头/表尾插入新结点123front45p1p2表头插入:p1->next=front;front=p1;表尾插入:back->next=p2;back=p2;注:若没有back指针,可通过循环找到链表的尾结点。back24第24页,共35页。25MainIndexContents单链表基本操作3.如何在链表表头/表尾删除结点12345frontbackp表头结点删除:p=front;front=front->next;deletep;表尾结点删除:(首先通过循环找到q结点,倒数第2个结点)back=q->next;q->next=NULL;deleteback;q3.如何在链表表头/表尾删除结点25第25页,共35页。26MainIndexContents单链表基本操作Insert:newNode->next=curr;Prev->next=newNode;Delete:node<T>*curr,*prev;prev->next=curr->next;deletecurr;4.如何在链表中任意位置插入/删除结点(需前驱指针/当前指针共同推进,prev/curr)26第26页,共35页。27MainIndexContents单链表小结1.如何创建链表(正向、反向创建)2.如何在链表表头插入新结点、删除头结点(链表维护有表尾结点/没有表尾结点)3.如何在链表表尾插入新结点、删除表尾结点

(链表维护有表尾结点/没有表尾结点)4.如何在链表中任意位置插入/删除结点(需前驱指针/当前指针共同推进,prev/curr)5.如何遍历全部链表/如何查找某个结点6.如何连接链表7.如何拆分链表8.如何删除链表27第27页,共35页。28MainIndexContents9.RemovingaTargetNode(实例)注意:前驱和当前指针,交替推进28第28页,共35页。29MainIndexContentsTemplate<TypenameT>VoideraseValue1(node<T>*front,constT&target){ node<T>*curr=front,*prev=Null; while(curr!=Null){ if(curr->nodeValue==target) { if(prev==Null) front=front->next; else prev->next=curr->next; deletecurr; } else{ prev=curr; curr=curr->next;} } }29第29页,共35页。30MainIndexContents思考:删除链表中重复元素:Template<typenameT>VoidremoveDuplicate(node<T>*front){ TcurrValue; node<T>*curr,*p; curr=front; while(curr!=NULL) { currValue=curr->dataValue; p=curr; p=p->next;

eraseValue1(p,currValue); curr=curr->next; }}10.Removingduplicates30第30页,共35页。31MainIndexContentsErasingnodesindescendingorder#include<iostream>#include"d_node.h"#include"d_nodel.h"#include"d_random.h"usingnamespacestd;template<typenameT>node<T>*getMax(node<T>*front);template<typenameT>voideraseValue(node<T>*&front,constT&target);31第31页,共35页。32MainIndexContentsErasingnodesindescendingorderintmain(){ node<int>*front=NULL,*p; randomNumberrnd; intlistCount,i; cout<<"Enterthesizeofthelist:"; cin>>lis

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论