码迷,mamicode.com
首页 > 编程语言 > 详细

java学习笔记——可用链表

时间:2015-06-21 13:08:50      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:

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学习笔记——可用链表

标签:

原文地址:http://www.cnblogs.com/lukexwang/p/4591834.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!