下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、精选优质文档-倾情为你奉上链表的代码: /节点class node object data; node next;/指向下一个结点 import java.io.; public class list /用变量来实现表头/ private node head=null; private node tail=null; private node pointer=null; private int length=0; public void deleteall() /清空整个链表/ head=null; tail=null; pointer=null; length=0; public void
2、reset() /链表复位,使第一个结点 成为当前结点/ pointer=null; public boolean isempty() /判断链表是否为空/ return(length=0); public boolean isend() /判断当前结点是否 为最后一个结点/ if(length=0) throw new java.lang.nullpointerexception(); else if(length=1) return true; else return(cursor()=tail); public object nextnode() /返回当前结点的下一个结点的值, 并使其
3、成为当前结点/ if(length=1) throw new java.util.nosuchelementexception(); else if(length=0) throw new java.lang.nullpointerexception(); else node temp=cursor(); pointer=temp; if(temp!=tail) return(temp.next.data); else throw new java.util.nosuchelementexception(); public object currentnode() /返回当前结点的值/ nod
4、e temp=cursor(); return temp.data; public void insert(object d) /在当前结点前插入一个结点, 并使其成为当前结点/ node e=new node(d); if(length=0) tail=e; head=e; else node temp=cursor(); e.next=temp; if(pointer=null) head=e; else pointer.next=e; length; public int size() /返回链表的大小/ return length; public object remove() /将当
5、前结点移出链表,下一个结点成为当前结点,如果移出的结点是最后一个结点,则第一个结点成为当前结点/ object temp; if(length=0) throw new java.util.nosuchelementexception(); else if(length=1) temp=head.data; deleteall(); else node cur=cursor(); temp=cur.data; if(cur=head) head=cur.next; else if(cur=tail) pointer.next=null; tail=pointer; reset(); else
6、pointer.next=cur.next; length; return temp; private node cursor() /返回当前结点的指针/ if(head=null) throw new java.lang.nullpointerexception(); else if(pointer=null) return head; else return pointer.next; public static void main(string args) /链表的简单应用举例/ list a=new list (); for(int i=1;i<=10;i) a.insert(n
7、ew integer(i); system.out.println(a.currentnode(); while(!a.isend() system.out.println(a.nextnode(); a.reset(); while(!a.isend() a.remove(); a.remove(); a.reset(); if(a.isempty() system.out.println(there is no node in list n); system.in.println(you can press return to quitn); try system.in.read(); /确保用户看清程序运行结果 catch(ioexception e) class node /构成链表的结点定义/ object data; node next; node(object d) data=d; next=null; 读者还可以根据实际需要定义新的方法来对链表进行操作。双向链表可以用类似的方法实现只是结点的类增加了一个指向前趋结点的指针。 可
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 节能低碳绿色环保的倡议书
- 教学课件下载教学课件
- 粮油产品采购运输合同范本
- 高端会所装修服务合同
- 旅游景区户外装修合同
- 电影院液化气运输协议
- 软件开发居间服务合同样本
- 建筑材料供应居间合同模板
- 速食店装修合同
- 摄影器材运输合同样本
- 2024年01月广东清远市殡仪馆招考聘用编外工作人员笔试近6年高频考题难、易错点荟萃答案带详解附后
- 中老年功能性食品
- 标准预防及分级防护
- 《VB-For循环语句》说课稿
- TL-PMM180超低烟尘使用及维护培训
- 大数据与企业资源管理
- 浅谈犯罪嫌疑人在审讯中保持沉默的应对策略
- 急诊科的职业风险与个人防护
- 汽车底盘构造与检修(第2版) 课件全套 项目1-4 绪论、传动系统的构造与检修-制动系统的构造与检修
- 颈椎病的手术治疗效果评估
- 老年友善医疗机构(医院)建设
评论
0/150
提交评论