首页 > 分享 > 简单链表类(宠物商店模型)

简单链表类(宠物商店模型)

简单链表类(宠物商店模型)

最新推荐文章于 2021-10-21 09:23:20 发布

dududu1224 于 2017-08-15 21:06:36 发布

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

interface ILink{ } class Link implements ILink{ private class Node{ private Object data; private Node next; public Node(Object data){ this.data=data; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; } //this=当前节点 public void addNode(Node node){ if(this.next==null){//当前节点的下一个为空 this.next=node; }else{ this.next.addNode(node); } } public void toArrayNode(){ Link.this.reData[Link.this.foot++]=this.data; if(this.next!=null){ this.next.toArrayNode(); } } public boolean containsNode(Object search){ if(this.data.equals(search)){ return true; }else{ if(this.next!=null){ return this.next.containsNode(search); }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 newdata){ if(Link.this.foot++==index){ this.data=newdata; }else{ this.next.setNode(index, newdata); } } //previous=this.root //this=root.next public void removeNode(Object data,Node previous){ if(this.data.equals(data)){ previous.next=this.next ; }else{ this.next.removeNode(data, this); } } } //------------------------------ private Object []reData; private int foot; private int count=0; private Node root;//根节点 public void add(Object data){//数据增加 if(data==null){ return;//方法调用结束 } Node node=new Node(data); if(this.root==null){ this.root=node; }else{ this.root.addNode(node); } this.count++; } public int size(){//取得元素个数 return this.count; } public boolean isEmpty(){//检验数组是否为空 return this.root==null&&this.count==0; } public Object [] toArray(){ if(this.count==0){ return null; } this.reData=new Object[this.count];//声明数组长度 this.foot=0; this.root.toArrayNode(); return this.reData; } public boolean contains(Object search){ if(search==null&&this.root==null){ return false; }else{ return this.root.containsNode(search); } } public Object get(int index){ if(index>=this.count){ return "wrong"; }else{ this.foot=0; return this.root.getNode(index); } } public void set(int index,Object newdata){ if(index>=this.count&&newdata==null){ return; }else{ this.foot=0; this.root.setNode(index, newdata); } } 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(data, this.root); }this.count--; } } } interface Pets{ public String getName(); public String getColor(); public int getAge(); } class PetShop{ private Link pets=new Link();//开辟一个链表,保存多个宠物 public void add(Pets pet){ this.pets.add(pet); } public void delet(Pets pet){ this.pets.remove(pet); } public Link getPets(){ return this.pets; } public Link search(String keyWord){ Link result=new Link(); Object []data=this.pets.toArray(); for(int x=0;x<data.length;x++){ Pets pet=(Pets) data[x]; if(pet.getName().contains(keyWord)||pet.getColor().contains(keyWord)){ result.add(pet); } } return result; } } class Dog implements Pets{ private String name; private int age; private String color; @Override public String getName() { // TODO Auto-generated method stub return this.name; } @Override public String getColor() { // TODO Auto-generated method stub return this.color; } @Override public int getAge() { // TODO Auto-generated method stub return this.age; } public Dog(String name,String color,int age){ this.name=name; this.color=color; this.age=age; } public boolean equals(Object obj){ if(obj==null){ return false; }if(this==obj){ return true; }if(!(obj instanceof Dog)){ return false; } Dog pet=(Dog) obj; return this.name.equals(pet.name)&&this.age==pet.age&&this.color.equals(pet.color); } public String toString(){ return this.name+this.color+this.age; } } class Cat implements Pets{ private String name; private int age; private String color; @Override public String getName() { // TODO Auto-generated method stub return this.name; } @Override public String getColor() { // TODO Auto-generated method stub return this.color; } @Override public int getAge() { // TODO Auto-generated method stub return this.age; } public Cat(String name,String color,int age){ this.name=name; this.color=color; this.age=age; } public boolean equals(Object obj){ if(obj==null){ return false; }if(this==obj){ return true; }if(!(obj instanceof Dog)){ return false; } Cat pet=(Cat) obj; return this.name.equals(pet.name)&&this.age==pet.age&&this.color.equals(pet.color); } public String toString(){ return this.name+this.color+this.age; } } //============================================== public class test2 { public static void main(String args[]){ PetShop ps=new PetShop(); Dog dog1=new Dog("旺旺","witer",1); ps.add(dog1); ps.add(new Dog("hei","black",10)); ps.add(new Dog("金毛","black",10)); ps.add(new Dog("波斯猫","black",10)); ps.add(new Dog("加菲","black",10)); ps.delet(new Dog("金毛","black",10)); Link all=ps.search("witer"); Object[]data=all.toArray(); for(int x=0;x<data.length;x++){ System.out.println(data[x]); } Link all1=ps.getPets(); Object []result =all1.toArray(); for(int x=0;x<result.length;x++){ System.out.println(result[x]); } } }

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271

相关知识

链表实战之宠物商店
mysql第十章宠物商店 宠物商店代码
mysql 项目案例宠物商店
java宠物商店代码
12.10综合案例:宠物商店
今天我开始学习:PETSHOP3.0宠物商店(经典案例)
多项式加法:合并有序表与多项式链表操作
宠物商店信息管理系统(C++)
木制仿真模型 益智玩具 3D立体拼 宠物商店儿童玩具 建筑模型
一个宠物商店的程序c语言,C语言 宠物商店管理系统 实训报告

网址: 简单链表类(宠物商店模型) https://m.mcbbbk.com/newsview1082793.html

所属分类:萌宠日常
上一篇: 古牧幼犬每天应该吃多少狗粮?如何
下一篇: 半水栖龟的饲养方法