您的位置:首页 > 移动开发 > Objective-C

java综合之 链表 接口 Object对象

2016-03-22 01:15 381 查看
//如果链表的的数据类型是对象,则关于比较的方法全部改成compare()
//class Book{
//    private String title;
//    private double price;
//    public Book(String title,double price){
//        this.title = title;
//        this.price = price;
//    }
//    public String toString(){
//        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;
//    }
//}

//定义实现Link类
class Link{
//以下为内部类Node
//定义内部类Node为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 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 Object getNode(int index){
if(Link.this.foot++ == index)
return this.data;    //  返回当前结点
else{      //向后继续搜索
return this.next.getNode(index);
}
}
public void setNode(int index,Object data){
if(Link.this.foot++ == index){
this.data = data;
}
else{
this.next.setNode(index,data);
}
}
//要传递上一个节点以及数据
public void removeNode(Node previous,Object data){
if(this.data.equals(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();
}
public void printNode(){
if(this != null){
System.out.println(this.data);
if(this.next != null)
this.next.printNode();
else
return ;
}
else
return;
}
}
//===============以上为内部类==================
private Node root;                      //根节点
private int count = 0;                  //保存元素的个数
private int foot = 0;                   //元素脚标
private Object[] retArray;
public void add(Object data){
if(data == null)
return;
Node newNode = new Node(data);
if(this.root == null)               //根节点不存在
this.root = newNode;
else{                               //根节点存在 向后继续寻找添加
this.root.addNode(newNode);
}
this.count++;                       //每次保存后数据量+1
}
public int size(){                      //返回数组长度
return this.count;
}
public boolean isEmpty(){               //链表判空
return this.count == 0;
}
public Object get(int index){          //按照下标查询
if(index >this.count)
return null;                   //无数据返回
this.foot = 0 ;                    //表示从前向后取
return this.root.getNode(index);
}
//第一次调用为root
//第二次调用为root.next
public boolean contains(Object data){
//没有要查询的数据,根节点也没有数据
if(data == null || this.root == null){
return false;
}
return this.root.containsNode(data);
}
public void set(int index,Object data){
if(index>this.count)
return;
this.foot = 0;
this.root.setNode(index,data);
}
public void remove(Object data){
if(this.contains(data)){         //判断是否存在
//要删除的节点是否是根节点
if(this.root.data.equals(data)){
this.root = this.root.next;
}
else{
this.root.next.removeNode(this.root,data);
}
this.count--;
}
}
public Object[] toArray(){
if(this.root == null)
return null;
this.foot = 0;
this.retArray = new Object [this.count];
this.root.toArrayNode();
return this.retArray;
}
public void printList(){          //输出链表所有的值
this.root.printNode();
}
}
//实现宠物商店
interface Pet{    //定义一个宠物的标准
public String getName();        //得到宠物的名字
public int getAge();            //得到宠物年龄
}
class PetShop{    //宠物商店类
private Link pets = new Link(); //多个宠物用链表存
public void add(Pet pet){       //宠物上架
this.pets.add(pet);
}
public void remove(Pet pet){    //下架
this.pets.remove(pet);
}
public Link search(String keyWord){    //实现模糊查询 因为不知道结果有什么 返回一个链表
Link result = new Link();    //保存结果
//将集合变为对象数组返回
Object obj [] = this.pets.toArray();
for(int i = 0; i < obj.length; i++){
Pet p = (Pet) obj[i];
if(p.getName().contains(keyWord)){         //使用contains实现
result.add(p);
}
}
return result;
}
}
class Cat implements Pet{
private String name;
private int age;
public Cat(String name,int age){
this.name = name;
this.age = age;
}
public String getName(){
return this.name;
}
public int getAge(){
return this.age;
}
public boolean equals(Object obj){
if(this == obj)
return true;
if(obj == null)
return false;
if(!(obj instanceof Cat))
return false;
Cat c = (Cat) obj;
if(this.name.equals(c.name) && this.age == c.age)
return true;
return false;
}
public String toString(){
return "猫的名字: "+this.name + " 猫的年龄: " + this.age + " 岁";
}
}
class Dog implements Pet{
private String name;
private int age;
public Dog(String name,int age){
this.name = name;
this.age = age;
}
public String getName(){
return this.name;
}
public int getAge(){
return this.age;
}
public boolean equals(Object obj){
if(this == obj)
return true;
if(obj == null)
return false;
if(!(obj instanceof Fish))
return false;
Dog c = (Dog) obj;
if(this.name.equals(c.name) && this.age == c.age)
return true;
return false;
}
public String toString(){
return "狗的名字: "+this.name + " 狗的年龄: " + this.age + " 岁";
}
}
class Fish implements Pet{
private String name;
private int age;
public Fish(String name,int age){
this.name = name;
this.age = age;
}
public String getName(){
return this.name;
}
public int getAge(){
return this.age;
}
public boolean equals(Object obj){
if(this == obj)
return true;
if(obj == null)
return false;
if(!(obj instanceof Fish))
return false;
Fish c = (Fish) obj;
if(this.name.equals(c.name) && this.age == c.age)
return true;
return false;
}
public String toString(){
return "鱼的名字: "+this.name + " 鱼的年龄: " + this.age + " 岁";
}
}
public class LinkedList{
public static void main(String args[]){
PetShop shop = new PetShop();
shop.add(new Cat("A猫",10));
shop.add(new Cat("B猫",10));
shop.add(new Dog("A狗",10));
shop.add(new Dog("B狗",10));
shop.add(new Fish("A鱼",10));
shop.add(new Fish("B鱼",10));
Link all = shop.search("狗");
Object [] list = all.toArray();
for(Object tmp : list)
System.out.println(tmp);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: