Java 链表(宠物商店)

范例:宠物商店

//宠物接口类
interface Pet {
	public abstract String getName();// 获取名字抽象方法

	public abstract int getAge();// 获取年龄抽象方法
}

// 猫类
class Cat implements Pet {
	private String name;// 猫的名字
	private int age;// 猫的年龄

	public Cat(String name, int age) {
		this.name = name;
		this.age = age;
	}

	@Override
	public boolean equals(Object obj) {// 覆写equals方法
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof Cat)) {
			return false;
		}
		Cat cat = (Cat) obj;// 向下强转
		if (this.name.equals(cat.name) && this.age == cat.age) {
			return true;
		}
		return false;

	}

	@Override
	public String getName() {
		return this.name;
	}

	@Override
	public int getAge() {
		return this.age;
	}

	@Override
	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;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof Dog)) {
			return false;
		}
		Dog dog = (Dog) obj;
		if (this.name.equals(dog.name) && this.age == dog.age) {
			return true;
		}
		return false;

	}

	@Override
	public String getName() {
		return this.name;
	}

	@Override
	public int getAge() {
		return this.age;
	}

	@Override
	public String toString() {
		return "狗狗的名字:" + this.name + ";年龄" + this.age + "。";
	}
}

// 宠物店类
class PetShop {
	private Link pets = new Link();

	// 1.宠物上架
	public void add(Object obj) {
		this.pets.add(obj);
	}

	// 2.宠物下架
	public void remove(Object obj) {
		this.pets.remove(obj);
	}

	// 3.货架模糊查询
	public Link search(String keyWord) {
		Link result = new Link();
		Object[] data = this.pets.toArray();
		for (int i = 0; i < data.length; i++) {
			Pet pet = (Pet) data[i];
			if (pet.getName().contains(keyWord)) {
				result.add(pet);
			}
		}
		return result;
	}
}

// 链表类
class Link {
	class Node {// 定义节点类
		private Node next;// 引用关系
		private Object data;// 保存数据

		// 构造方法
		public Node(Object data) {
			this.data = data;
		}

		// 1.添加节点
		public void addNode(Node newNode) {
			if (this.next == null) {// 当前节点的下一个节点为空
				this.next = newNode;
			} else {// 向后继续保存
				this.next.addNode(newNode);
			}
		}

		// 2.查询数据
		public boolean containsNode(Object data) {
			if (data.equals(this.data)) {// 当前节点数据为要查询的数据
				return true;// 后面不在需要查询
			} else {// 当前节点不能满足查询
				if (this.next != null) {// 判断后续是否还有节点
					return this.next.containsNode(data);
				} else {// 没后后续节点
					return false;
				}
			}
		}

		// 3.数据修改
		public void setNode(int index, Object data) {
			// 使用自定义foot内容与 index比较
			if (Link.this.foot++ == index) {
				this.data = data;// 进行内容修改
			} else {// 继续
				this.next.setNode(index, data);
			}
		}

		// 4.获取数据
		public Object getNode(int index) {
			// 使用自定义foot内容与 index比较
			// foot自增,目的为下一次查询方便
			if (Link.this.foot++ == index) {// 要查询的索引
				return this.data;// 返回当前节点数据
			} else {// 继续向后查
				return this.next.getNode(index);
			}
		}

		// 5.删除数据
		public void removeNode(Node previous, Object data) {
			if (data.equals(this.data)) {// 数据对应
				previous.next = this.next;// 空出当前节点
			} else {// 继续向后查询
				this.next.removeNode(this, data);
			}
		}

		// 6.对象数组
		public void toArrayNode() {
			Link.this.retArray[Link.this.foot++] = this.data;
			if (this.next != null) {// 后续还有元素
				this.next.toArrayNode();
			}
		}
	}

	// ================以上为内部类================
	private Node root;// 根节点
	private int count = 0;// 保存元素个数
	private int foot = 0;
	private Object[] retArray;// 返回数组

	// 1.添加数据
	public void add(Object data) {
		if (data == null) {// 数据不允许为null
			return;
		}
		Node newNode = new Node(data);// 保存数据,数据打包
		if (this.root == null) {// 当前没有根节点
			this.root = newNode;// 新节点为根节点
		} else {// 根节点存在,其它节点给Node处理
			this.root.addNode(newNode);
		}
		this.count++;// 每次保存成功后自增一次
	}

	// 2.查询数据
	public boolean contains(Object data) {
		if (data == null || this.root == null) {// 没有数据,根节点也没有数据
			return false;
		}
		return this.root.containsNode(data);
	}

	// 3.修改数据
	public void set(int index, Object data) {
		if (index > this.count) {
			return;
		}
		this.foot = 0;// 归零,从头向后开始
		this.root.setNode(index, data);// 修改过程交给Node
	}

	// 4.获取数据
	public Object get(int index) {
		if (index > this.count) {// 超出查询范围
			return null;
		}
		this.foot = 0;// 归零,从头向后开始
		return this.root.getNode(index);// 查询过程交给Node
	}

	// 5.删除数据
	public void remove(Object data) {
		if (this.contains(data)) {// 1.先判断是否存在此数据
			// 2.判断删除的数据是否根节点数据
			// 3.root是Node的对象,此处直接访问内部类的私有操作
			if (data.equals(this.root.data)) {// 根节点是否满足删除
				this.root = this.root.next;// 空出当前节点
			} else {// 不是根节点
				// 根节点的下一个节点开始判断
				this.root.next.removeNode(this.root, data);
			}
			this.count--;// 删除成功后自减
		}
	}

	// 6.链表长度
	public int length() {
		return this.count;
	}

	// 7.链表是否为空
	public boolean isEmpty() {
		return this.count == 0;
	}

	// 8.对象数组
	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 static void print(Object[] data) {
		for (Object x : data) {
			System.out.println(x + " ");
		}
		System.out.println();
	}

}

public class Demo04 {
	public static void main(String[] args) {
		PetShop ps = new PetShop();
		ps.add(new Cat("加菲猫", 5));
		ps.add(new Cat("秋山猫", 5));
		ps.add(new Cat("无毛猫", 7));
		ps.add(new Dog("柴犬", 4));
		ps.add(new Dog("秋田犬", 3));
		ps.add(new Dog("猎犬", 6));
		Link all = ps.search("秋");
		Object[] data = all.toArray();
		Link.print(data);
	}
}

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值