interface Link {
public void add(Object data) ;
public Object [] toArray() ;
}
class LinkImpl implements Link { // 外部的程序只关心此类
private class Node { // 使用私有内部类,防止外部使用此类
private Object data ;
private Node next ;
public Node(Object data) {
this.data = data ;
}
public void addNode(Node newNode) {
if (this.next == null) {
this.next = newNode ;
} else {
this.next.addNode(newNode) ;
}
}
public void toArrayNode() {
LinkImpl.this.retData [LinkImpl.this.foot ++] = this.data ;
if (this.next != null) {
this.next.toArrayNode() ;
}
}
}
private Node root ;
private int foot = 0 ;
private int count = 0 ;
private Object retData [] = null ;
public void add(Object data) {
if (data == null) { // 现在没有要增加的数据
return ; // 结束调用
}
Node newNode = new Node(data) ; // 创建新的节点
if (this.root == null) { // 保留有根节点
this.root = newNode ;
} else { // 应该交由Node类负责处理
this.root.addNode(newNode) ;
}
this.count ++ ;
}
public Object [] toArray() {
if (this.root == null) { // 没有数据
return null ;
}
this.retData = new Object [this.count] ;
this.foot = 0 ;
this.root.toArrayNode() ;
return this.retData ;
}
}
public class TestDemo {
public static void main(String args[]) {
Link all = new LinkImpl() ;
all.add("A") ;
all.add("B") ;
all.add("C") ;
Object obj [] = all.toArray() ;
for (int x = 0 ; x < obj.length ; x ++) {
System.out.println(obj[x]) ;
}
}
}
二叉树:
package cn.mldn.demo; import java.util.Arrays; class Person implements Comparable<Person> { private String name ; private int age ; public Person(String name,int age) { this.name = name ; this.age = age ; } @Override public String toString() { return "姓名:" + this.name + ",年龄:" + this.age + "\n" ; } @Override public int compareTo(Person o) { return this.age - o.age ; } } class BinaryTree { // 二叉树的实现类 private class Node { // 数据结构中必须有Node类,负责保存数据以及节点的关系匹配 @SuppressWarnings("rawtypes") private Comparable data ; private Node left ; // 比根节点小的内容 private Node right ; // 比根节点大或者相等的内容 @SuppressWarnings({ "rawtypes", "unused" }) public Node(Comparable data) { this.data = data ; } @SuppressWarnings("unchecked") public void addNode(Node newNode) { if (this.data.compareTo(newNode.data) > 0) { // 保存在左边 if (this.left == null) { this.left = newNode ; } else { this.left.addNode(newNode); } } else { if (this.right == null) { this.right = newNode ; } else { this.right.addNode(newNode); } } } public void toArrayNode() { if (this.left != null) { // 有左节点 this.left.toArrayNode(); } BinaryTree.this.retData[BinaryTree.this.foot ++] = this.data ; if (this.right != null) { this.right.toArrayNode(); } } } // ******************* 编写BinaryTree操作 ********************** private Node root ; // 必须保留住根节点 private int count = 0 ; // 保存节点个数 private int foot = 0 ; private Object [] retData ; @SuppressWarnings("rawtypes") public void add(Object obj) { // 数据的追加 Comparable data = (Comparable) obj ; Node newNode = new Node(data) ; // 将数据包装在Node节点之中 if (this.root == null) { // 保存根节点 this.root = newNode ; } else { this.root.addNode(newNode) ; } this.count ++ ; } public Object [] toArray() { if (this.count > 0) { this.foot = 0; this.retData = new Object [this.count] ; this.root.toArrayNode() ; return this.retData ; } else { return null ; } } } public class TestDemo { public static void main(String args[]) throws Exception { BinaryTree bt = new BinaryTree() ; bt.add(new Person("小陶",39)); bt.add(new Person("小施",29)); bt.add(new Person("小暄",10)); System.out.println(Arrays.toString(bt.toArray())); } } |