版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、链式存储结构的实现,表(List)的链式存储结构单链表,单链表部分操作的实现,小结和作业,复习顺序存储结构的实现,顺序存储结构,用一组地址连续的存储单元依次存放线性表中的数据元素,a0 a1 ai-1 ai an-1,线性表的起始地址(基地址),ublic class MyArrayList private AnyType theItems; /存储空间基址 private int theSize; /表的大小 private static final int DEFAULT_CAPACITY = 10; /数组容量 成员方法 ,线性表顺序存储结构的实现,线性表顺序存储结构的实现,构造函数My
2、ArrayList( ),get(int idx),set(int idx,AnyType newVal),add(AnyType x)、add(int idx,AnyType x),remove(int idx),add,原型: public void add(int idx,AnyType x),作用:把x插入线性表,作为第idx个数据元素, 即在顺序表的第idx个位置之前插入新的 数据元素x。,逻辑结构的变化, , ,(a0, , aidx-1, aidx, , an-1) (a0, , aidx-1, x, aidx, , an-1),add,存储结构的变化,add(2, x),6,t
3、heSize,7,theSize,add,过程: 1、判断i的合法性 2、移动 3、赋值 4、theSize+,add,add合法性,A,10,一般的0itheSize,如果theItems.length=theSize?,增加存储容量,if (idxtheSize) throw new LocateException(idx); if (theItems.length=theSize) ensureCapacity(size( )*2+1); /增加存储容量,add移动,6,theSize,7,theSize,a6a5 a5a4 a4a3 a3a2,ak = ak+1,k=(theSize-
4、1) idx,add移动,for (int i=theSize-1;i=idx;i-) theItemsi+1=theItems i; theItemsidx=x;,add(4, 66),theSize-1,0,87,56,42,66,42,56,87,add,ublic void add(int idx,AnyType x) throws LocateException /在顺序表的第idx个位置之前插入新的数据元素x if (idxtheSize-1) throw new LocateException(idx); if( theItems.length = size( ) ) ensur
5、eCapacity( size( ) * 2 + 1 ); /增加存储容量 for( int i = theSize-1; i =idx; i- ) theItems i+1 = theItems i ; theItems idx = x; theSize+; ,add,1、在什么情况下,移动次数最小,最小移动次数是多少?,2、在什么情况下,移动次数最多,最多移动次数是多少?,3、在插入0theSize-1每个位置的概率相同的情况下,移动次数的期望值是多少?,原型: public AnyType remove(int idx),作用:删除表中的第idx个位置上的数据元素,remove,remo
6、ve,逻辑结构的变化, ,(a1, , ai-1, ai, ai+1, an) (a1, , ai-1, ai+1, , an),存储结构的变化,remove(2),6,theSize,5,theSize,remove,过程: 1、判断i的合法性 2、取值 3、移动 4、theSize-,remove,a2a3 a3a4 a4a5,ak-1 = ak,k=i +1 theSize-1,6,theSize,5,theSize,remove移动,remove,ublic AnyType remove( int idx ) throw LocateException /删除表中的第idx个位置上的数
7、据元素 if (idxtheSize) throw new LocateException(idx); AnyType removedItem = theItems idx ; for( int i = idx+1; i theSize- 1; i+ ) theItems i-1 = theItems i ; theSize-; return removedItem; ,1、在什么情况下,移动次数最小,最小移动次数是多少?,2、在什么情况下,移动次数最多,最多移动次数是多少?,3、在删除0theSize-1每个位置的概率相同的情况下,移动次数的期望值是多少?,remove,链式存储结构的实现,
8、1、每个数据元素使用一个节点表示,2、每个数据元素在物理存储空间可以不相邻,3、数据元素之间的线性关系由指针链接保证,1、节点类:,private class Node ,public AnyType data; public Node next;,public Node(AnyType d,Node n), this.data=d; this.next=n; ,public Node(AnyType d), this.data=d; this.next=null; ,public Node( ), this.data=null; this.next=null; ,链式存储结构的实现,Node
9、 p,q; p=new Node(12); q=new Node(13); p.next=q;,Node q=new Node(13); Node p=new Node(12,q);,链式存储结构的实现,null,null,Node firstNode=null;,当firstNode=null时,表示空链表。,null,firstNode,链式存储结构的实现,L,2、单链表类,链式存储结构的实现,ublic class SingleLinkedList private int theSize; /表的大小 private Node firstNode; /第一个节点 成员方法 ,链式存储结构
10、的实现,2、单链表类,单链表部分操作的实现,构造函数SingleLinkedList( ),get(int idx),set(int idx,AnyType newVal),add(AnyType x)、add(int idx,AnyType x),remove(int idx),merge函数,contains(AnyType x,SingleLinkedList La),构造函数SingleLinkedList,public SingleLinkedListt( ) ,原型: public SingleLinkedList ( ),作用: 形成一个空表,this. firstNode =n
11、ull;,theSize=0;,get,原型: public AnyType get( int idx ),作用: 返回第idx个数据元素的值,public AnyType get( int idx ) return getNode( idx ).data; ,get(3),=firstNode,定位到aidx需要移动idx次指针,p=p.next,get(0),get,get,private Node getNode( int idx ) ,p = firstNode;,for( int i = 0; i idx; i+ ) p = p.next;,return p;,if( idx siz
12、e() ) throw new IndexOutOfBoundsException( getNode index: + idx + ; size: + size( ) );,Node p;,1、在什么情况下,移动指针的次数最少,2、在什么情况下,移动指针的次数最多,如何改善?,get,set,原型: public AnyType set( int idx, AnyType newVal ),作用: 给表中第idx个数据元素赋新值newVal,返 回赋值前的数据元素的值。,getNode(2),set(2,x),set,p.data=x,p= firstNode,x,set,public Any
13、Type set( int idx, AnyType newVal ) ,for( int i = 0; i idx; i+ ) p = p.next;,AnyType oldVal=p.data; p.data=newVal;,if( idx size() ) /抛出异常,Node p= firstNode;,return oldVal;,add,原型: public boolean add( AnyType x ),作用:将x插入到链表。,默认为最后一个节点,即add(size( ),x),add,public boolean add( AnyType x ) add( size( ),
14、x ); return true; ,add,原型: public boolean add( int idx,AnyType x ),作用:将x插入到链表,作为第idx个节点,逻辑结构的变化, , ,(a0, , aidx-1, aidx, , an-1) (a0, , aidx-1, e, aidx, , an-1),存储结构的变化,过程 1、建立一个含有数据元素x的新节点。 2、找到第idx-1个节点 3、调整,将新节点插入到链表中。,add,过程 1、建立一个含有数据元素x的新节点。,add,Node newNode=new Node(x),newNode,过程 2、找到第idx-1个节
15、点。,add,Node p=getNode(idx-1),过程 3、调整,将新节点插入到链表中。,add,newNode,newNode.next=p.next,p.next=newNode,add,public void add( int idx, AnyType x ) ,Node p=getNode(idx-1); newNode.next = p.next; p.next = newNode; theSize+;,Node newNode = new Node( x );,1、idx=0,2、idx=theSize,3、空表,add的边界问题,1、idx=0时,newNode.next
16、=p.next;,p.next=newNode;,a0,a1,a2,p,x,newNode,add的边界问题,L,newNode.next=firstNode,firstNode=newNode;,a0,a1,a2,L,x,newNode,如果 idx=0,则过程如下:,add的边界问题,firstNode,2、firstNode =null,即为空表,不能插入,p= firstNode,p,newNode.next=p.next;,p.next=newNode;,add的边界问题,firstNode,add的边界问题,2、firstNode=null时的操作过程为:,newNode.next
17、=firstNode,firstNode=newNode;,add,public void add( int idx, AnyType x ) ,Node p=getNode(idx-1); newNode.next = p.next; p.next = newNode; theSize+;,Node newNode = new Node( x );,if(this.firstNode=null|idx=0) newNode.next=firstNode; firstNode=newNode; theSize+; else ,remove,原型:public boolean remove(in
18、t idx),作用: 删除第idx个节点,aidx,aidx+1,(a0, , ai-1, ai, ai+1, , an), ,(a1, , ai-1, ai+1, , an),逻辑结构的变化:,存储结构的变化:,q=p.next p.next = q.next; return true;,aidx+1,p,q,remove,p=getNode(idx-1),public boolean remove( int idx) Node p=getNode(idx-1),q; q=p.next; p.next = q.next; theSize-; return true; ,remove,2、id
19、x=0,4、idx=theSize-1,1、空表,3、表中只有一个元素,remove的边界问题,q=p.next; p.next = q.next;,无法进行删除操作,a1,a2,L,2、idx=0时的操作过程为:,p=firstNode;,firstNode=p.next;,remove的边界问题,a0,L,p,p=firstNode firstNode=null=p.next;,如何判断只有一个数据元素?,theSize=1或者 firstNode.next = = null,3、表中只有一个元素,remove的边界问题,public boolean remove( int idx) No
20、de p=getNode(idx-1),q; q=p.next; p.next = q.next; theSize-; return true; ,remove,if(firstNode=null) return false; /空表 if(idx=0 | firstNode.next=null) /只有一个数据元素,或idx=0 Node p=this. firstNode; firstNode=p.next; theSize-; else ,contains,原型:public int contains(AnyType x),作用:测试值x是否在单链表中,如果在返回其所在 的位置,否则返回
21、-1,.data与x进行比较,ublic int contains(AnyType x) int idx=0; if(firstNode=null) return -1; /空表 Node p=firstNode; while(p.data!=x) return idx; ,contains,idx+; p=p.next;,if(p=null) return -1;,merge,原型:public boolean merge(SingleLinkedList La, SingleLinkedList Lb),作用: 把Lb的数据元素插入到La的表尾,a0,a1,a2,La,b0,b1,Lb,m=Lb.get(i); La.add(m);,public boolean merge(SingleLinkedList La,SingleLinkedList Lb) for(int i=0;iLb.theSize;i+) return tru
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 中山大学附属第三医院2026年合同人员招聘备考题库完整答案详解
- 2026年工程进度控制合同
- 2025年湖南省中西医结合医院湖南省中医药研究院附属医院高层次人才公开招聘13人备考题库带答案详解
- 2026年废灭蚁灵污染易发区保护保险合同中
- 2026年湿地公园保护保险合同中
- 2025年中国航空工业集团有限公司招聘备考题库带答案详解
- 2025年上饶市广信区人民法院公开招聘劳务派遣工作人员14人备考题库及参考答案详解
- 2026年兴业银行海口分行秋季校园招聘备考题库及参考答案详解一套
- 2025 九年级语文下册戏剧矛盾冲突设计课件
- 2025湖南长沙市食品药品检验所公开招聘编外合同制人员12人备考核心题库及答案解析
- 飞机机务维修工程师航空业机务维修绩效表
- 2026届四川省德阳市2023级高三一诊英语试题(含答案和音频)
- 2025年遵守工作纪律财经纪律心得体会
- 第11课《我们都是热心人》第一课时(课件)
- 7.2《走向未来》课件- 2024-2025学年统编版道德与法治九年级下册
- 市场销售费用管理制度(3篇)
- 新教科版科学四年级上册分组实验报告单
- 雷达截面与隐身技术课件
- 长期护理保险技能比赛理论试题库300题(含各题型)
- IATF-I6949SPC统计过程控制管理程序
- GB/T 4458.2-2003机械制图装配图中零、部件序号及其编排方法
评论
0/150
提交评论