public class LinkDemo {
public static void main(String[] args){
Link all=new Link();
all.add(new Book("Java开发实战经典",79.8));
all.add(new Book("Oracle开发实战经典",89.8));
all.add(new Book("Android开发实战经典",99.8));
System.out.println("保存书的个数:"+all.size());
System.out.println(all.contains(new Book("Java开发实战经典",79.8)));
all.remove(new Book("Android开发实战经典",99.8));
Book [] books=all.toArray();
for(int x=0;x<books.length;x++){
System.out.println(books[x].getInfo());
}
}
}
class Book{
private String title;
private double price;
public Book(String title,double price){
this.title=title;
this.price=price;
}
public boolean compare(Book book){
if(book==null){
return false;
}
if(this==book){
return true;
}
if(this.title.equals(book.title) && this.price==(book.price)){
return true;
}
else{
return false;
}
}
public String getInfo(){
return "图书信息:"+this.title+",图书价格:"+this.price;
}
}
class Link {
private class Node {
private Book data;
private Node next;
public Node(Book data) {
this.data = data;
}
public void addNode(Node newNode) {
if (this.next == null) {
this.next = newNode;
} else {
this.next.addNode(newNode);
}
}
public boolean containsNode(Book data) {
if (data.compare(this.data)) {
return true;
} else {
if (this.next != null) {
return this.next.containsNode(data);
} else {
return false;
}
}
}
public Book getNode(int index) {
if (Link.this.foot++==index){
return this.data;
}
else{
return this.next.getNode(index);
}
}
public void setNode(int index,Book data){
if(Link.this.foot++==index){
this.data=data;
}
else{
this.next.setNode(index,data);
}
}
public void removeNode(Node previous,Book data){
if(data.compare(this.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 = 0;
private Book[] retArray;
public void add(Book data){
if(data==null){
return;
}
Node newNode=new Node(data);
if(this.root==null){
this.root=newNode;
}
else{
this.root.addNode(newNode);
}
this.count++;
}
public int size(){
return this.count;
}
public boolean isEmpty(){
return this.count==0;
}
public boolean contains(Book data){
if(data==null || this.root==null){
return false;
}
return this.root.containsNode(data);
}
public Book get(int index){
if(index>this.count){
return null;
}
this.foot=0;
return this.root.getNode(index);
}
public void set(int index,Book data){
if(index>this.count){
return;
}
this.foot=0;
this.root.setNode(index,data);
}
public void remove(Book data){
if(this.contains(data)){
if(data.equals(this.root.data)){
this.root=this.root.next;
}
else{
this.root.next.removeNode(this.root,data);
}
this.count--;
}
}
public Book[] toArray(){
if(this.root==null){
return null;
}
this.foot=0;
this.retArray=new Book[this.count];
this.root.toArrayNode();
return this.retArray;
}
public void clear(){
this.root=null;
this.count=0;
}
}
网友评论