链表综合实例(宠物商店)

现在假设有一个宠物商店,里面有出售各种宠物,要求可以实现宠物的上架、下架处理,也可以根据关键字查询出宠物的信息。

  1. 定义出宠物的标准
    interface Pet	//定义宠物的标准
    {
    	public String getName();	//名字
    	public String getColor();	//颜色
    }

     

  2. 宠物商店应该以宠物的标准为主
    class PetShop //宠物商店
    {
    	private ILink<Pet> allPets = new LinkImpl<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 LinkImpl<Pet>();	//保存查询结构
    		Object[] result = this.allPets.toArray();	//获取全部数据
    		if(result != null){
    			for(Object obj : result){
    				Pet pet = (Pet) obj;	//向下转型
    				if(pet.getName().contains(keword)||
    					pet.getColor().contaiins(keword)){
    					searchResult.add(pet);	//保存查询结构
    				}
    			}
    		}
    		return searchResult;
    	}	
    }

     

  3. 根据宠物的标准来定义宠物信息
    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 boolean equals(Object obj){
    		if(obj == null){
    			return false;
    		}
    		if(!(obj instanceof Cat)){	//如果obj不是Cat的实例
    			return false;
    		}
    		if(this == obj){
    			return true;
    		}
    		Cat cat = (Cat) obj;
    		return this.name.equals(cat.name) && this.color.equals(cat.color);
    	}
    	public String toString(){
    		return "【宠物猫】名字:" +this.name+"\t颜色:"+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 boolean equals(Object obj){
    		if(obj == null){
    			return false;
    		}
    		if(!(obj instanceof Dog)){	//如果obj不是Cat的实例
    			return false;
    		}
    		if(this == obj){
    			return true;
    		}
    		Dog dog = (Dog) obj;
    		return this.name.equals(dog.name) && this.color.equals(dog.color);
    	}
    	public String toString(){
    		return "【宠物狗】名字:" +this.name+"\t颜色:"+this.color;
    	}
    }

     

  4. 实现宠物商店的操作
    class  JavaDemo
    {
    	public static void main(String[] args) 
    	{
    		PetShop petshop= new PetShop();
    		petshop.add(new Dog("大黄","黄色"));
    		petshop.add(new Dog("小黄","浅黄色"));
    		petshop.add(new Cat("大白","白色"));
    		Object[] result = petshop.search("黄").toArray();
    		for (Object obj : result)
    		{
    			System.out.println(obj);
    		}
    	}
    }
    
    interface ILink <E>	//设置泛型避免安全隐患
    {
    	public void add(E data);	//增加
    	public void remove(E data);	//根据数据删除
    	public Object[] toArray();	//遍历
    	public Object find(E data);	//删除
    
    }
    
    class LinkImpl<E> implements ILink<E>
    {
    	//---------内部链表类---------
    	private class Node{
    		private E data;
    		private Node next;
    	
    		public Node(E data){
    			this.data = data;
    		}
    
    		//添加
    		public void addNode(Node newNode){
    			if(this.next == null){	//当前对象为空则存放数据
    				this.next = newNode;	
    			}else{
    				this.next.addNode(newNode);	//不为空再继续下一个对象,递归调用。	
    			}
    		}
    		//查找
    		public Object findNode(E data){
    			if(this.data.equals(data)){	//如果与当前对象数据相等,返回当前对象
    				return this;
    			}
    			return this.next.findNode(data);
    		}
    		//删除
    		public void removeNode(Node lastNode,E data){
    			if(this.data.equals(data)){
    				lastNode.next = this.next;	//上一个节点与下一个节点相连
    			}
    			if(this.next != null){
    				this.next.removeNode(this,data);	//递归调用
    			}
    		}
    		//遍历
    		public void toArrayNode(){
    			LinkImpl.this.returnData[LinkImpl.this.foot++] = this.data;	//LinkImpl下的当前对象的foot随着链表长度递增并记录数据
    			if(this.next != null){
    				this.next.toArrayNode();	//递归调用直到没有下一个链表对象
    			}
    		}
    	}
    
    	//-----------外部类-----------
    	private Node root;//定义根(第一个Node)
    	private int count = 0;	//数组长度
    	private Object[] returnData;
    	private int foot; 
    
    	//添加
    	public void add(E data){
    		Node newNode = new Node(data);
    		if(data ==null){
    			return;
    		}
    		if(this.root == null){	//根为空数据放根里
    			this.root = newNode;	
    		}else{
    			root.addNode(newNode);	//根不为空下则根的下一个链表对象调用
    		}
    		count++;
    	}
    	//查找
    	public Object find(E data){
    		if (data == null)
    		{
    			return null;
    		}
    		if(root.data.equals(data)){	//	如果数据等于root的数据,返回root对象
    			return root;
    		}
    		return root.next.findNode(data);
    	}
    	//删除
    	public void remove(E data){
    		if(data == null){
    			return;
    		}
    		if(root.data.equals(data)){	//如果数据等于root的数据,root等于下一个对象
    			root = root.next;
    		}
    		root.next.removeNode(this.root,data);
    		count--;
    	}
    	//遍历
    	public Object[] toArray(){
    		this.returnData =  new Object[this.count];	//按照链表长度创建存储的数组
    		this.foot = 0;	//创建脚标用来控制数组
    		this.root.toArrayNode(); 
    		return returnData;
    	}
    
    }
    interface Pet	//定义宠物的标准
    {
    	public String getName();	//名字
    	public String getColor();	//颜色
    }
    
    class PetShop //宠物商店
    {
    	private ILink<Pet> allPets = new LinkImpl<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 LinkImpl<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 boolean equals(Object obj){
    		if(obj == null){
    			return false;
    		}
    		if(!(obj instanceof Cat)){	//如果obj不是Cat的实例
    			return false;
    		}
    		if(this == obj){
    			return true;
    		}
    		Cat cat = (Cat) obj;
    		return this.name.equals(cat.name) && this.color.equals(cat.color);
    	}
    	public String toString(){
    		return "【宠物猫】名字:" +this.name+"\t颜色:"+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 boolean equals(Object obj){
    		if(obj == null){
    			return false;
    		}
    		if(!(obj instanceof Dog)){	//如果obj不是Cat的实例
    			return false;
    		}
    		if(this == obj){
    			return true;
    		}
    		Dog dog = (Dog) obj;
    		return this.name.equals(dog.name) && this.color.equals(dog.color);
    	}
    	public String toString(){
    		return "【宠物狗】名字:" +this.name+"\t颜色:"+this.color;
    	}
    }

    【宠物狗】名字:大黄    颜色:黄色
    【宠物狗】名字:小黄    颜色:浅黄色

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值