NO 链表方法名称 描述
1 public void add(数据类型 对象) 向链表中增加数据
2

public int size()

查看链表中数据个数
3 public boolean isEmpty() 查看链表是否为空
4 public void clean() 清空链表
5 public 数据类型 get(int index) 返回指定索引的数据对象,需要使用自定义类中的Compare()函数方法
6 public boolean contains(数据类型  对象) 查看链表中是否包含数据对象,需要使用自定义类中的Compare()函数方法
7 public  void remove(数据类型 对象) 删除链表中数据对象,需要使用自定义类中的Compare()函数方法
8 public 数据类型[] toArray() 转换为数据对象数组
class Person{
String name;
int age;
public Person(String name,int age) {
// TODO Auto-generated constructor stub
this.name=name;
this.age=age;
}
public boolean Compare(Person person){
if(person==null) return false;
if(this==person) return true;
if(this.name==person.name&&this.age==person.age)
return true;
return false;
}
public void getInfo(){
System.out.println("name:"+name+",age:"+age);
}
}
class Link{
private class Node{
private Person data;
private Node next;
public Node(Person data) {
// TODO Auto-generated constructor stub
this.data=data;
}
/**********************************************/
public void addNode(Node newNode){
if(this.next==null)
this.next=newNode;
else {
this.next.addNode(newNode);
}
}
/*********************************************/
public Person getNode(int index){
if(index==Link.this.foot++)//注意:内部类能直接访问外部类的变量
return this.data;
return this.next.getNode(index);
}
/*********************************************/
public boolean containsNode(Person data){
if(this.data.Compare(data))
return true;
else if(this.next==null)/*重点*/
return false;
return this.next.containsNode(data);
}
public void removeNode(Node previous,Person data){
if(this.data.Compare(data))
previous.next=this.next;
else
this.next.removeNode(this, data);
}
public void toarrayNode(){
Link.this.retArray[Link.this.foot++]=this.data;
if(this.next!=null)
this.next.toarrayNode();
}
/*********************************************/
}
private Node root;
private int count=0;//结点个数
private int foot;//为了查找相关位置节点
private Person [] retArray;
public void add(Person data){
Node newNode=new Node(data);
if(root==null) root=newNode;
else {
root.addNode(newNode);
}
count++;
}
public int size(){
return count;
}
public boolean isEmpty(){
return this.count==0;
}
public void clean(){//java的虚拟机会自动回收那些分配的空间
root=null;
count=0;
}
public Person get(int index){
if(index>count)
return null;
this.foot=0;
return this.root.getNode(index);
}
public boolean contains(Person data){
if(data==null) return false;
return this.root.containsNode(data);
}
public void remove(Person data){
if(this.contains(data)){
if(this.root.data==data)
this.root=this.root.next;
else
this.root.next.removeNode(root, data);//重点
this.count--;
}
}
public Person [] toArray(){
if(this.count==0)
return null;
retArray=new Person[count];
this.foot=0;
this.root.toarrayNode();
return retArray;
}
}
public class LinkDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Link link=new Link();
link.add(new Person("张三", 20));
link.add(new Person("李四", 21));
link.add(new Person("王五", 22));
System.out.println("size:"+link.size());
Person[] per1=link.toArray();
for(int i=0;i<per1.length;i++)
per1[i].getInfo();
Person tmp=new Person("李四", 21);
link.remove(tmp);
Person[] per2=link.toArray();
for(int i=0;i<per2.length;i++)
per1[i].getInfo();
} }

java学习笔记——可用链表的更多相关文章

  1. 《Java学习笔记(第8版)》学习指导

    <Java学习笔记(第8版)>学习指导 目录 图书简况 学习指导 第一章 Java平台概论 第二章 从JDK到IDE 第三章 基础语法 第四章 认识对象 第五章 对象封装 第六章 继承与多 ...

  2. Java学习笔记--Swing用户界面组件

    很多与AWT类似. 事件处理参考:Java学习笔记--AWT事件处理 1.设计模式: 模型:存储内容视图:显示内容控制器:处理用户输入· 2. 文本输入常用组件 2.1 文本域: JLabel lab ...

  3. Java学习笔记4

    Java学习笔记4 1. JDK.JRE和JVM分别是什么,区别是什么? 答: ①.JDK 是整个Java的核心,包括了Java运行环境.Java工具和Java基础类库. ②.JRE(Java Run ...

  4. java学习笔记16--I/O流和文件

    本文地址:http://www.cnblogs.com/archimedes/p/java-study-note16.html,转载请注明源地址. IO(Input  Output)流 IO流用来处理 ...

  5. java学习笔记10--泛型总结

    java学习笔记系列: java学习笔记9--内部类总结 java学习笔记8--接口总结 java学习笔记7--抽象类与抽象方法 java学习笔记6--类的继承.Object类 java学习笔记5-- ...

  6. 20145230《java学习笔记》第七周学习总结

    20145230 <Java程序设计>第7周学习总结 教材学习内容 Lambda语法概览 我们在许多地方都会有按字符串长度排序的需求,如果在同一个方法内,我们可以使用一个byName局部变 ...

  7. 20145231第二周Java学习笔记

    20145231 <Java程序设计>第2周学习总结 教材学习内容总结 本周的学习采用的依然是先看课本,再看视频,然后实践敲代码,最后根据学习笔记总结完成博客. 第三章:基础语法 知识点比 ...

  8. Java学习笔记之---API的应用

    Java学习笔记之---API的应用 (一)Object类 java.lang.Object 类 Object 是类层次结构的根类.每个类都使用 Object 作为超类.所有对象(包括数组)都实现这个 ...

  9. java学习笔记(1)java的基础介绍 、JDK下载、配置环境变量、运行java程序

    java工程师是开发软件的 什么是软件呢? 计算机包括两部分: 硬件: 鼠标.键盘.显示器.主机箱内部的cpu.内存条.硬盘等 软件: 软件包括:系统软件和应用软件 系统软件:直接和硬件交互的软件:w ...

随机推荐

  1. 理解S12(X)架构中的地址映射方案

    目录 1. 介绍 2. CPU 本地地址 3. 分页窗口 4. 内存页 5. 控制各个对象在内存中放置的位置 介绍 在一个S12或S12X架构中,很有必要分清楚两种类型的内存地址:banked和non ...

  2. Jquery CheckBox 选中和非选中

    if($("input[name='is_pay']").prop('checked')) { $("input[name='is_pay']").prop(' ...

  3. 生成DLL

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...

  4. [bzoj1026][SCOI2009]windy数——数位dp

    题目 求[a,b]中的windy数个数. windy数指的是任意相邻两个数位上的数至少相差2的数,比如135是,134不是. 题解 感觉这个题比刚才做的那个简单多了...这个才真的应该是数位dp入门题 ...

  5. FIRST SCRAPY PRJ

    zpc@Lenovo-PC:/prj/pyscrapy/a$ scrapy startproject helloword New Scrapy project 'helloword' created ...

  6. ScrollView下嵌套GridView或ListView默认不在顶部的解决方法

    自定义ScrollView重写下面的方法 /* ScrollView下嵌套GridView或ListView默认不在顶部的解决方法*/ @Override protected int computeS ...

  7. Kubernetes镜像制作

    #将需要安装的包全部放入一个目录下,然后开始编写Dockerfile#Dockerfile格式FROM #依赖的镜像MAINTAINER #制作者信息WORKDIR #工作目录,打包启动镜像后的所在目 ...

  8. springboot webapi 支持跨域 CORS

    1.创建corsConfig 配置文件 @Configuration public class corsConfig { @Autowired varModel varModel_; @Bean pu ...

  9. 开发框架 springBoot

    1.多个环境的配置文件 在application.yml 中配置需要调用的配置文件 spring: profiles: active: dev 运行方式的,先运行application.yml 再根据 ...

  10. Codeforces 914D Bash and a Tough Math Puzzle (ZKW线段树)

    题目链接  Round #458 (Div. 1 + Div. 2, combined)  Problem D 题意  给定一个序列,两种询问:单点修改,询问某个区间能否通过改变最多一个数使得该区间的 ...