来自左神书中的一道题,不过左神的Java代码有几个问题,自己亲自Coding实现了一遍,麻烦小伙伴们帮忙转转,让左神下一版时能纠正这个错误,另,转载请注明出处哈。
题目:
已知有宠物、狗、猫类如下,实现一种猫狗队列的结构,要求实现add、pollAll、pollDog、pollCat、isEmpty、isDogEmpty、isCatEmpty等方法。
class Pet {
private String type;
public Pet(String type) {
this.type = type;
}
public String getType() {
return this.type;
}
}
class Dog extends Pet {
public Dog() {
super("Dog");
}
}
class Cat extends Pet {
public Cat() {
super("Cat");
}
}
分析:本题考查实现特殊数据结构及针对特殊功能的算法设计能力。
现在我们定义新类PetEnterQueue,将不同实例盖上时间戳,同时不改变原来的类。
class PetEnterQueue {
private Pet pet;
private long count;
public PetEnterQueue(Pet pet, long count) {
this.pet = pet;
this.count = count;
}
public Pet getPet() {
return this.pet;
}
public long getCount() {
return this.count;
}
public String getEnterPetType() {
return this.pet.getType();
}
}
我们实现的队列就是PetEnterQueue类的实例,首先有一个不断累加的数据项count用来表示实例进队列的时间,同时有两个队列,一个只存放Dog,另一个只存放Cat。
public class DogCatQueue {
private Queue<PetEnterQueue> dogQ;
private Queue<PetEnterQueue> catQ;
private static long count;
public DogCatQueue() {
this.dogQ = new LinkedList<PetEnterQueue>();
this.catQ = new LinkedList<PetEnterQueue>();
this.count = 0;
}
···
}
add:(左神之前用的add,我改为offer,更适用于队列)
public void add(Pet pet) {
if (pet.getType().equals("Dog")) {
this.dogQ.offer(new PetEnterQueue(pet, ++this.count));
System.out.println("add:"
+ ((LinkedList<PetEnterQueue>) dogQ).peekLast()
.getEnterPetType() + "-"
+ ((LinkedList<PetEnterQueue>) dogQ).peekLast().getCount());
} else if (pet.getType().equals("Cat")) {
this.catQ.offer(new PetEnterQueue(pet, ++this.count));
System.out.println("add:"
+ ((LinkedList<PetEnterQueue>) catQ).peekLast()
.getEnterPetType() + "-"
+ ((LinkedList<PetEnterQueue>) catQ).peekLast().getCount());
} else {
throw new RuntimeException("Error:Not Dog or Cat");
}
}
pollAll:(左神之前用的if,那样跑一次就没了,应该用while,同时左神忘了清空count,我在末尾补上了)
public void pollAll() {
if (this.dogQ.isEmpty() && this.catQ.isEmpty()) {
System.out.println("Queue is impty.");
}
while (!this.dogQ.isEmpty() || !this.catQ.isEmpty()) {
if (!this.dogQ.isEmpty() && !this.catQ.isEmpty()) {
if (this.dogQ.peek().getCount() < this.catQ.peek().getCount()) {
System.out.println("pollAll:"
+ dogQ.peek().getEnterPetType() + "-"
+ dogQ.peek().getCount());
this.dogQ.poll();
} else {
System.out.println("pollAll:"
+ catQ.peek().getEnterPetType() + "-"
+ catQ.peek().getCount());
this.catQ.poll();
}
} else if (!this.dogQ.isEmpty()) {
System.out.println("pollAll:" + dogQ.peek().getEnterPetType()
+ "-" + dogQ.peek().getCount());
this.dogQ.poll();
} else if (!this.catQ.isEmpty()) {
System.out.println("pollAll:" + catQ.peek().getEnterPetType()
+ "-" + catQ.peek().getCount());
this.catQ.poll();
}
}
this.count = 0;
}
pollDog:(因为题目中要求将所有Dog实例依次弹出,因此应该用while,左神用了if)
public void pollDog() {
if (this.dogQ.isEmpty()) {
System.out.println("Dog Queue is impty.");
}
while (!this.dogQ.isEmpty()) {
System.out.println("poll:" + dogQ.peek().getEnterPetType() + "-"
+ dogQ.peek().getCount());
this.dogQ.poll();
}
}
pollCat:(因为题目中要求将所有Cat实例依次弹出,因此应该用while,左神用了if)
public void pollCat() {
if (this.catQ.isEmpty()) {
System.out.println("Cat Queue is impty.");
}
while (!this.catQ.isEmpty()) {
System.out.println("poll:" + catQ.peek().getEnterPetType() + "-"
+ catQ.peek().getCount());
this.catQ.poll();
}
}
接下来是3个判空函数:
public void isEmpty() {
System.out.println("isEmpty:"
+ Boolean.toString((this.dogQ.isEmpty() && this.catQ.isEmpty())));
}
public void isDogQueueEmpty() {
System.out.println("isDogQueueEmpty:"
+ Boolean.toString(this.dogQ.isEmpty()));
}
public void isCatQueueEmpty() {
System.out.println("isCatQueueEmpty:"
+ Boolean.toString(this.catQ.isEmpty()));
}
最后简单的跑一下测试代码:
public static void main(String[] args) {
// TODO Auto-generated method stub
DogCatQueue q = new DogCatQueue();
q.add(new Dog());
q.add(new Cat());
q.isEmpty();
q.pollAll();
q.isEmpty();
q.add(new Dog());
q.add(new Cat());
q.add(new Dog());
q.add(new Cat());
q.isDogQueueEmpty();
q.pollDog();
q.isDogQueueEmpty();
q.isCatQueueEmpty();
q.pollCat();
q.isCatQueueEmpty();
}
输出结果:
add:Dog-1
add:Cat-2
isEmpty:false
pollAll:Dog-1
pollAll:Cat-2
isEmpty:true
add:Dog-1
add:Cat-2
add:Dog-3
add:Cat-4
isDogQueueEmpty:false
poll:Dog-1
poll:Dog-3
isDogQueueEmpty:true
isCatQueueEmpty:false
poll:Cat-2
poll:Cat-4
isCatQueueEmpty:true
左神犀利,学习了,不过左神也有疲惫的时候,我百度左神的联系方式(邮箱、QQ、微信、微博等),然而并没有找到,希望各位小伙伴们帮忙转一下,让左神尽早看到,下一版中勘正这个错误。
谢谢左神,我们也会在我们的算法之路上越走越远!
另,Coding不易,转载请注明出处哈。
权兴权意
http://blog.csdn.net/hxqneuq2012/article/details/52680161