Java day18【第三十章】单链表案例实战

【第三十章】单链表案例实战

一.宠物商店:

现在假设一个宠物商店,里面可以出售各种宠物,要求可以实现宠物的上架,下架处理,以及查询宠物的信息。

interface ILink<E>{
    public void add(E date);
    public int getsize();
    public boolean isEmpty();
    public Object[] toArray();
    public E getdate(int index);
    public void set(int index,E date);
    public boolean contain(E date);
    public void remove(E date);
    public void clean();
}
class SetLink<E> implements ILink<E>{
    private int size;
    private Node<E> root;
    public Object [] returnDate;
    private int foot ;
    //定义节点类
    public class Node<E>{
        E date;
        Node<E> next;
        Node<E> previous;
        public Node(E date, Node<E> next) {
            this.date = date;
            this.next = next;
        }
        
        //获取链表数据辅助
        public void toArraynode() {
            SetLink.this.returnDate[SetLink.this.foot ++] = this.date;
            if(this.next != null) {
                this.next.toArraynode();
            }
        }
        //辅助根据索引获取数据
        public E getnode(int index) {
            if(SetLink.this.foot ++ == index) {
                return this.date;
            }else {
                return this.next.getnode(index);
            }
        }
        
        //找到索引位置.
        public void setnode(int index,E date) {
            if(SetLink.this.foot == index) {
                this.date= date;
            }else {
                this.next.setnode(index,date);;
            }
        }
        
        //判断数据存在
        public boolean containnode(E date) {
            if(this.date.equals(date)) {
                return true;
            }else {
                if(this.next == null) {
                    return false;
                }else {
                    return this.next.containnode(date);
                }
            }
        }
        
        //删除节点
        public void removeNode(Node<E> previous,E date) {
            if(this.date.equals(date)) {
                previous.next = this.next;
            }else {
                if(this.next!=null) {
                    this.next.removeNode(this,date);
                }
            }
        }
    }
    //增加数据
    public void add(E date) {
        if(root == null) {
            root = new Node<E>(date , null);
        }else {
            Node<E> current = root;
            while(current.next != null) {
                current = current.next;
            }
            current.next = new Node<E>(date,null);
        }
        size ++;
    }
    //获取链表长度
    public int getsize() {
        return size;
    }
    //判断链表是否为空
    public boolean isEmpty() {
        return this.size ==0;
    }
    //将数据以数组形式返回
    public Object[] toArray() {
        if(root == null) {
            return null;
        }
        this.foot = 0;
        this.returnDate = new Object[this.size] ;
        this.root.toArraynode();
        return returnDate;
    }    
    //获取索引位置的数据:
    public E getdate(int index) {
        if(index > this.size) {
            return null;
        }
        this.foot = 0;
        return this.root.getnode(index);
    }
    //修改指定位置的数据
    public void set(int index , E date) {
        this.foot = 0;
        this.root.setnode(index,date) ;
    }
    //判断指定数据是否存在
    public boolean contain(E date) {
        return this.root.containnode(date);
    }
    //数据删除
    public void remove(E date) {
        if(this.contain(date)) {
            if(this.root.date.equals(date)) {
                this.root = this.root.next;
            }else {
                this.root.next.removeNode(this.root, date);
            } 
            this.size--;
        }
    }
    //清空链表:
    public void clean() {
        this.root = null ;
        this.size = 0;
    }
}

interface Pet{
    public String getname();
    public String getcolor();
}
class Petshop{
    private ILink<Pet> allpets = new SetLink<Pet>();
    public void add(Pet pet) {
        this.allpets.add(pet);
    }
    public void delete(Pet pet) {
        this.allpets.remove(pet);
    }
    public ILink<Pet> search(String keyword){
        ILink<Pet> searchresult = new SetLink<Pet>();
        Object result[] = this.allpets.toArray();
        if(result !=null ) {
            for(Object obj:result) {
                Pet pet = (Pet)obj;
                if(pet.getname().contains(keyword)||
                    pet.getcolor().contains(keyword)) {
                    searchresult.add(pet);
                }
            }
        }
        return searchresult;
    }
}
class Cat implements Pet{
    private String name;
    private String color;
    public Cat(String name,String color) {
        this.name = name;
        this.color = color;
    }
    public String getname() {
        return this.name;
    }
    public String getcolor() {
        return this.color;
    }
    public String toString() {
        return "【宠物猫】名字:"+this.name+"、颜色:"+this.color;
    }
}
class Dog implements Pet{
    private String name;
    private String color;
    public Dog(String name,String color) {
        this.name = name;
        this.color = color;
    }
    public String getname() {
        return this.name;
    }
    public String getcolor() {
        return this.color;
    }
    public String toString() {
        return "【宠物狗】名字:"+this.name+"、颜色:"+this.color;
    }
}
public class ListCon{
    public static void main(String args[]) {
        Petshop shop = new Petshop();
        shop.add(new Cat("侯","Blue"));
        shop.add(new Dog("侯h","Green"));
        shop.add(new Cat("侯hh","Green"));
        shop.add(new Cat("侯hhh","Yellow"));
        Object result[] = shop.search("h").toArray();
        for(Object obj : result) {
            System.out.println(obj);
        }
        }
}

二. 超市购物车:

  使用面向对象的概念表示出下面的生活场景:小明去超市买东西,所有买到的东西都放在了购物车,最后到收银员结账。

猜你喜欢

转载自www.cnblogs.com/xiwenxinaini/p/11827157.html