链表
class Phone {
private String brand ;
private double price ;
public Phone(String brand,double price) {
this.brand = brand ;
this.price = price ;
}
// 无参构造、setter与getter略
public String toString() {
return "品牌:" + this.brand + ",价格:" + this.price ;
}
public boolean equals(Object obj) {
if (this == obj) {
return true ;
}
if (obj == null) {
return false ;
}
if (!(obj instanceof Phone)) {
return false ;
}
Phone ph = (Phone) obj ;
return this.brand.equals(ph.brand) && this.price == ph.price ;
}
}
interface ILink {
public void add(Object data) ; // 这个方法向链表之中保存对象
public int size() ; // 取得链表的长度
public boolean isEmpty() ; // 判断该链表是否有数据
public Object get(int index) ; // 根据索引取得链表中保存的数据
public boolean contains(Object obj) ;// 查询某个数据是否存在
public void remove(Object obj) ; // 删除不需要的数据
public void set(int index,Object data) ; // 根据索引修改指定数据
public void clean() ; // 清空链表
public Object [] toArray() ; // 变为对象数组后输出
}
class LinkImpl implements ILink {
// Node类可以描述出对象保存的先后顺序
private class Node { // 这个类用户不需要知道,只有本类使用
private Object data ; // 保存数据
private Node next ; // 保存下一个节点
// 保证每一个Node创建的时候都有要保存的数据
public Node(Object data) {
this.data = data ;
}
// 第1次执行:this = LinkImpl.root
// 第2次执行:this = LinkImpl.root.next
// 第3次执行:this = LinkImpl.root.next.next
public void addNode(Node newNode) { // 设置节点关系
if (this.next == null) { // 当前节点之后有位置
this.next = newNode ; // 保存新节点
} else { // 如果没有位置
this.next.addNode(newNode) ;
}
}
// 第1次执行:this = LinkImp.root
// 第2次执行:this = LinkImp.root.next
public Object getNode(int index) {
if (LinkImpl.this.foot ++ == index) {
return this.data ;
} else {
return this.next.getNode(index) ;
}
}
// 第1次:this = LinkImpl.root.next,previous = LinkImp.root
public void removeNode(Node previous,Object obj) {
if (this.data.equals(obj)) {
previous.next = this.next ;
} else { // 之前已经判断过要删除的数据存在
this.next.removeNode(this,obj) ;
}
}
public boolean containsNode(Object data) {
if (this.data.equals(data)) {
return true ;
} else {
if (this.next != null) {
return this.next.containsNode(data) ;
} else {
return false ;
}
}
}
public void setNode(Object obj,int index) {
if (LinkImpl.this.foot ++ == index) {
this.data = obj ;
} else {
this.next.setNode(obj,index) ;
}
}
public void toArrayNode() {
LinkImpl.this.retObj [LinkImpl.this.foot ++] = this.data ;
if (this.next != null) {
this.next.toArrayNode() ;
}
}
}
// *******************************
private Node root ; // 表示根节点
private int count = 0 ;
private int foot = 0 ; // 查找使用
private Object retObj [] ; // 返回的对象数组
// ************* 覆写接口中的方法 **********
public void add(Object data) {
// 1、数据本身不包含有先后的顺序
// 需要将数据包装在Node类对象之中,因为Node可以描述先后关系
if (data == null) { // 数据为null
return ; // 结束调用
}
// 2、将数据包装在Node类对象之中
Node newNode = new Node(data) ;
// 3、处理根元素
if (this.root == null) { // 现在没有根元素
this.root = newNode ; // 将第一个节点保存为根元素
} else {
this.root.addNode(newNode) ;// 交由Node处理
}
this.count ++ ;
}
public int size() {
return this.count ;
}
public boolean isEmpty() {
return this.count == 0 && this.root == null ;
}
public Object get(int index) {
if (this.isEmpty()) { // 为空元素
return null ;
}
if (index >= this.count) { // 超过了保存数量
return null ;
}
this.foot = 0 ;
return this.root.getNode(index) ;
}
public boolean contains(Object obj) {
if (this.isEmpty()) {
return false ;
}
return this.root.containsNode(obj) ;
}
public void remove(Object obj) {
if (!this.contains(obj)) { // 数据不存在
return ; // 结束方法
}
// 链表里面最关注的是根节点
if (this.root.data.equals(obj)) { // 根节点要删除
this.root = this.root.next ; // 下一个作为根节点
} else { // 根节点判断完了
this.root.next.removeNode(this.root,obj) ;
}
this.count -- ;
}
public void set(int index,Object data){
this.foot = 0 ;
if (this.isEmpty()) { // 为空元素
return ;
}
if (index >= this.count) { // 超过了保存数量
return ;
}
this.root.setNode(data,index) ;
}
public void clean() {
this.root = null ;
this.count = 0 ;
}
public Object [] toArray() {
if (this.isEmpty()) {
return null ;
}
this.foot = 0 ;
this.retObj = new Object [this.count] ;
this.root.toArrayNode() ;
return this.retObj ;
}
}
public class TestDemo {
public static void main(String args[]) {
ILink all = new LinkImpl() ;
// System.out.println("保存数据个数:" + all.size() + ",是否为空链表:" + all.isEmpty()) ;
all.add(new Phone("黑米",19.8)) ;
all.add(new Phone("紫米",29.8)) ;
Object data [] = all.toArray() ;
for (int x = 0 ; x < data.length ; x ++) {
System.out.println(data[x]) ;
}
// all.set(0,new Phone("绿米",39.8)) ;
// all.clean() ;
// System.out.println(all.get(0)) ;
// all.remove(new Phone("黑米",19.8)) ;
// System.out.println("保存数据个数:" + all.size() + ",是否为空链表:" + all.isEmpty()) ;
// System.out.println(all.get(0)) ;
// System.out.println(all.get(2)) ;
// System.out.println(all.contains(new Phone("黑米",19.8))) ;
// System.out.println(all.contains(new Phone("大米",19.8))) ;
}
}