宠物商店(链表应用)

//链表!
interface ILink<E>{  //链表接口,定义若干链表的操作
	public void add(E e); //插入数据
	public int size(); //获取链表长度
	public boolean isEmpty(); //判空
	public Object[] toArray(); //返回链表数据,用数组接收
	public E get(int index); //获取链表指定位置上的数据
	public void set(int index,E data);
	public  boolean contains(E data);
	public void remove(E data);
	public void clean();
}

class LinkImpl<E> implements ILink<E>{
	private class Node<E>{
		private E data ; 
		private Node<E> next ;
		public Node(E data) {
			this.data = data ;
		}
		public void addNode(Node<E> newNode) {
			if(this.next == null) {
				this.next = newNode ;
			}else {
				this.next.addNode(newNode);
			}
		}
		public void toArrayNode() {
			LinkImpl.this.returnData[LinkImpl.this.foot++] = this.data;
			if(this.next!=null) {
				this.next.toArrayNode();
			}
		}
		public E getNode(int index) {
			if(LinkImpl.this.foot++ == index) {
				return this.data;
			}else {
				return this.next.getNode(index);
			}
		}
		public void setNode(int index,E data) {
			if(LinkImpl.this.foot++ == index) {
				this.data = data ;
			}else {
				this.next.setNode(index, data);
			}
		}
		public boolean containsNode(E data) {
			if(this.data.equals(data)) {
				return true;
			}else {
				if(this.next == null) {
					return false;
				}else {
					return this.next.containsNode(data);
				}
			}
		}
		@SuppressWarnings("unchecked")
		public void removeNode(Node previous,E data) {
			if(this.data.equals(data)) {
				previous.next = this.next;
			}else {
				if(this.next != null) {
					this.next.removeNode(this, data);
				}
			}
			
		}
	}
	
	private Node<E> root ;  //根节点
	private int count; // 记录结点个数
	private int foot;
	private Object[] returnData;
	@Override
	public void add(E e) {
		if(e == null) {
			return ; // 插入链表的数据不能为空
		}
		Node<E> newNode = new Node<E>(e);
		if(this.root == null) {
			this.root = newNode;
		}else {
			this.root.addNode(newNode);
		}
		this.count++;
	}
	@Override
	public int size() {
		return this.count;
	}
	@Override
	public boolean isEmpty() {
//		if(this.count == 0) {
//			return true;
//		}else {
//			return false;
//		}
		return this.count == 0;
	}
	@Override
	public Object[] toArray() {
		if(this.isEmpty()) {
			throw new NullPointerException("集合内容为空");
		}
		this.foot = 0;
		this.returnData = new Object[this.count]; //先开辟数组
		this.root.toArrayNode();
		return this.returnData;
	}
//	@Override
//	public String get(int index) {
//		return (String)this.toArray()[index];
//	}
	@Override
	public E get(int index) {
		if(index >= this.count) {
			throw new ArrayIndexOutOfBoundsException("不正确的数据索引");
		}
		this.foot = 0 ;
		return this.root.getNode(index);
	}
	@Override
	public void set(int index,E data) {
		if(index >= this.count) {
			throw new ArrayIndexOutOfBoundsException("不正确的数据索引");
		}
		this.foot = 0 ;
		this.root.setNode(index, data);
	}
	@Override
	public boolean contains(E data) {
		if(data == null) {
			return false;
		}
		return this.root.containsNode(data);
	}
	@Override
	public void clean() {
		this.root = null;
		this.count = 0 ;
	}
	@Override
	public void remove(E data) {
		if(this.root.data.equals(data)) {
			this.root = this.root.next;
		}else {
			this.root.next.removeNode(this.root,data);
		}
		this.count--;
	}
}

interface IPet{
	public String getName();
	public String getColor();
}

class PetShop{
	private ILink<IPet> allPets = new LinkImpl<IPet>();
	public void add(IPet pet) {
		allPets.add(pet);
	}
	public void delete(IPet pet) {
		allPets.remove(pet);
	}
	public ILink<IPet> search(String keyword){
		ILink<IPet> searchResult = new LinkImpl<IPet>();
		Object result[] = this.allPets.toArray();
		if(result != null ) {
			for(Object obj : result) {
				IPet pet = (IPet) obj ;
				if(pet.getName().contains(keyword)||pet.getColor().contains(keyword)) {
					searchResult.add(pet);
				}
			}
		}
		return searchResult;
	}
}
class Cat implements IPet{
	private String name;
	private String color;
	public Cat(String name,String color) {
		this.color = color ;
		this.name = name ;
	}
	@Override
	public String getName() {
		return this.name;
	}
	@Override
	public String getColor() {
		return this.color;
	}
	@Override
	public boolean equals(Object obj) {
		if(obj == null) {
			return false;
		}
		if(!(obj instanceof Cat)) {
			return false;
		}
		if(this==obj) {
			return true;
		}
		Cat cat = (Cat) obj;
		return this.name.equals(cat.name)&&this.color.equals(cat.color);
	}
	@Override
	public String toString() {
		return "[Cat]"+"name:"+this.name+" color:"+this.color;
	}
}
class Dog implements IPet{
	private String name;
	private String color;
	public Dog(String name,String color) {
		this.color = color ;
		this.name = name ;
	}
	@Override
	public String getName() {
		return this.name;
	}
	@Override
	public String getColor() {
		return this.color;
	}
	@Override
	public boolean equals(Object obj) {
		if(obj == null) {
			return false;
		}
		if(!(obj instanceof Dog)) {
			return false;
		}
		if(this==obj) {
			return true;
		}
		Dog Dog = (Dog) obj;
		return this.name.equals(Dog.name)&&this.color.equals(Dog.color);
	}
	@Override
	public String toString() {
		return "[Dog]"+"name:"+this.name+" color:"+this.color;
	}
	
}
public class AlwaysDemo {
	public static void main(String args[]){
		PetShop shop = new PetShop();
		shop.add(new Dog("小黄","黄色"));
		shop.add(new Dog("小黑","黑色"));
		shop.add(new Dog("大黑","黑色"));
		shop.add(new Dog("小花","花色"));
		shop.add(new Cat("小黄","黄色"));
		shop.add(new Cat("小橘","橘色"));
		Object result[] = shop.search("黄").toArray();
		for(Object obj : result) {
			System.out.println(obj);
		}
		
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值