【第五节】Java代码实现链表
2015-10-13 10:46
148 查看
package com.java.chapterFive; public class LinkListApp { /** * @param args */ public static void main(String[] args) { LinkList thelist=new LinkList(); thelist.insertFirst(22, 2.99); thelist.insertFirst(44, 4.99); thelist.insertFirst(66, 6.99); thelist.insertFirst(88, 8.99); thelist.displayList(); Link f=thelist.find(44); if(f!=null) System.out.println("找到了关键值"+f.iData); else System.out.println("没有找到该值"); Link d=thelist.delete(66); if(d!=null) System.out.println("已经删除该值"+d.iData); else System.out.println("没有删除"); thelist.displayList(); } } /////////////////////////////////////////////////////////////////////////////////////////// class Link{ public int iData; public double dData; public Link next; public long LData; //---------------------------------------------------------------------------------------- public Link(int id,double d){ this.iData=id; this.dData=d; } public Link(long d){ LData=d; } //---------------------------------------------------------------------------------------- public void displayLink(){ System.out.println("{"+this.iData+" "+this.dData+"}"); } public void displayLongLink(){ System.out.print(LData+" "); } } /////////////////////////////////////////////////////////////////////////////////////// class LinkList{ public Link first; //------------------------------------------------------------------------------------------ public LinkList(){ first=null; } //---------------------------------------------------------------------------------------- public void insertFirst(int id,double dd){ Link newLink=new Link(id,dd); newLink.next=first; first=newLink; } //--------------------------------------------------------------------------------------- public Link find(int key){ Link current=first; while(current.iData!=key){ if(current.next==null) return null; else current=current.next; } return current; } //--------------------------------------------------------------------------------------- public Link delete(int key){ Link current=first; Link previous=first; while(current.iData!=key) { if(current.next==null) return null; else { previous=current; current=current.next; } } if(current==first) first=first.next; else previous.next=current.next; return current; } //---------------------------------------------------------------------------------------- public void displayList(){ System.out.println("List (first--->last): "); Link current=first; while(current!=null){ current.displayLink(); current=current.next; } System.out.println(" "); } //---------------------------------------------------------------------------------------- }
相关文章推荐
- JDBC(Java Database Connection) 教程
- java创建对象的四种方式
- 公共技术点之 Java 注解 Annotation
- 一个经典例子让你彻彻底底理解java回调机制
- java判断是否是周末
- [Java]Spring入门
- java里面的treemap与HashMap
- eclipse 中progect build target 列表为空的问题
- java初始化(static)总结
- spring使用TransactionProxyFactoryBean配置事务
- 关于java中父类引用子类实例化的问题
- JAVA DATE类型推断尺寸数据比较法
- [收藏]Spring入门视频教程:Introduction to Spring
- ES006-Elasticsearch-JAVA客户端
- Myeclipse8.5 反编译插件 jad 安装
- Java并发的四种风味:Thread、Executor、ForkJoin和Actor
- java对象和json对象之间互相转换
- Java核心技术第5章(7)
- 在同一台机器上安装多个版本jdk,修改环境变量不生效
- android studio 快捷键切换为eclipse的快捷键