您的位置:首页 > 编程语言 > Java开发

Java链表之基础应用

2017-04-16 16:31 246 查看
class Book{ //创建book 类
private String title;
private double price;
public Book(String title, double price){
this.title = title;
this.price = price;
}
public String getInfo(){
return "图书名称:" + this.title + ",图书价格:" + this.price;
}
public boolean compare(Book book){ //比较方法
if(this == book){
return true;
}
if(book == null){
return false;
}
if(this.title.equals(book.title) && this.price == book.price){
return true;
}
return false;
}
}
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);
}// 没有后续节点 , 返回false
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(previous.next, data);
}
}
public Book [] toArrayNode(){//转化为对象数组进行输出
Link.this.retArray[Link.this.foot ++] = this.data;
if(this.next != null){
this.next.toArrayNode();
}
return null;
}
}
//===============以上为内部类============
private Node root;
private int foot = 0; // 索引
private int count = 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); //增加判断转到Node()类
}
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){ //查询指定索引的内容
this.foot = 0;
if(index > count)
return null;//没有数据
else
return this.root.getNode(index);
}
public void set(int index,Book data){ //修改指定索引内容
this.foot = 0;
if(index > this.count)
return ; //结束
else
this.root.setNode(index,data);
}
public void remove(Book data){  // 删除指定内容的节点
if(this.contains(data)){	//contains保证数据存在
if(data.compare(this.root.data)){	//删除的为根节点
this.root = this.root.next;
}else{ //不是根节点,从第二个节点开始
this.root.next.removeNode(this.root, data);
}
this.count --;  //删除后个数-1
}
}
public Book [] toArray(){  //转化为对象数组进行输出(重要)
if(this.root == null){
return null;
}else{
this.foot = 0;
this.retArray  = new Book[this.count]; //开辟内存
this.root.toArrayNode();
}
return this.retArray;
}
}
public class Demo1 {
public static void main(String[] args) {

//测试
Link link = new Link();
link.add(new Book("Java开发",78.5));
link.add(new Book("Web前端",59.5));
link.add(new Book("JSP开发",85));
System.out.println("共保存书籍" + link.size()); //共保存书籍
System.out.println(link.contains(new Book("Web前端",59.5))); //查询书籍
link.remove(new Book("JSP开发",85)); //删除书籍
System.out.println("共保存书籍" + link.size()); //共保存书籍
Book [] books = link.toArray(); //转化到对象数组进行输出
for(int i = 0 ;i < books.length; i++)
System.out.println(books[i].getInfo());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息