链表的代码(共5页)_第1页
链表的代码(共5页)_第2页
链表的代码(共5页)_第3页
链表的代码(共5页)_第4页
链表的代码(共5页)_第5页
全文预览已结束

下载本文档

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

文档简介

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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论