链表出了问题。。。
class Book{//定义一个书类private String name;//书名
private double price;//价钱
public Book(String name,double price){
this.name = name;
this.price = price;
}
//比较
public boolean equals(Object obj){
if(this == obj){
return true;
}
if(obj == null){
return false;
}
if(!(obj instanceof Book)){
return false;
}
Book book = (Book)obj;
if(this.name.equals(book.name) && this.price == book.price){
return true;
}else{
return false;
}
}
public String toString (){
return "书名"+this.name+" ,书价钱"+this.price;
}
}
class Link{//定义链表
private class Node{//内部类
private Object date;
private Node next;
public Node(Object date){
this.date = date;
}
public void addNode(Node newNode){
if(this.next == null){
this.next = newNode;
}else{
this.next.addNode(newNode);
}
}
public boolean containsNode( Object date){
if(this.date.equals(date)){
return true;
}else
if(this.next == null){
return false;
}else{
return this.next.containsNode(date);
}
}
public Object getNode(int index){
if(Link.this.foot++ == index){
return date;
}else{
return this.next.getNode(index);
}
}
public void removeNode(Node p,Object date){
if(this.date.equals(date)){
p.next = this.next;//删除当前节点
}else{
this.next.removeNode(this,date);
}
}
public void toArrayNode(){
Link.this.retArray[Link.this.foot++] = date;
if(this.next != null){
this.next.toArrayNode();
}
}
}
private Node root;//根节点
private Node p;//上一个节点
private int count = 0;//个数
private int index;
private int foot = 0;
private Object [] retArray;
//添加
public void add(Object date){
Node newNode = new Node(date);//添加数据
if(this.root == null){//当根节点为空
this.root = newNode;
}else{
this.root.addNode(newNode);
}
count++;
}
//个数
public int size(){
return this.count;
}
//是否包含
public boolean contains(Object date){
if(this.root == null && date == null){
return false;
}else{
return this.root.containsNode(date);
}
}
//查询(索引)
public Object get(int index){
if(index >this.count){
return null;
}else{
this.foot = 0;
return this.root.getNode(index);
}
}
//删除节点
public void remove(Object date){
if(this.contains(date)){//包含数据
if(this.root.date.equals(date))//根节点相符
{
this.root = this.root.next;//删除根节点
}else{
this.root.removeNode(this.root,date);//交给下一个节点
}
}
this.count--;
}
//用数组输出
public Object[] toArray(Object date){
if(this.root == null){
return null;
}
this.foot = 0;
Object[] retArray = new String[this.count];//开辟组数
this.root.toArrayNode();
return this.retArray;
}
}
public class LinkDe{
public static void main(String[] args){
Link a = new Link();
a.add(new Book("java",12.1));
a.add(new Book("C",13.2));
a.add(new Book("c#",14.1));
System.out.println(a.size());
System.out.println(a.contains(new Book("java",12.1)));
a.remove(new Book("java",12.1));
System.out.println(a.get(0));
Object [] temp = a.toArray();
}
}