An animal shelter holds only dogs and cats, and operates ona strictly “first in, first out” basis. People must adopt either the
“oldest”(based on arrival time) of all animals at the shelter, or they can selectwhether they could prefer a dog or a cat (and will receive the oldest animal ofthat type). They cannot select which specific animal they would like. Createthe data structures
to maintain this system and implement operations such asenqueue, dequeueAny, dequeueDog and dequeueCat. You may use the built-inLinkedList data structure.
大意是有一个动物收养站,猫或狗都不停地被收养进来,同时也有人想领养。规则是领养人可以挑选猫或狗,但不能挑具体哪一只,只能挑选早被收养的那只动物。
思路:
分别建猫和狗两个队列就搞定了。
package Stack_Queue;
import java.util.LinkedList;
public class S3_7 {
// ============================== Animal
static abstract class Animal {
private int order;
protected String name;
public Animal(String n) {
name = n;
}
public abstract String name();
public void setOrder(int ord) {
order = ord;
}
public int getOrder() {
return order;
}
public boolean isOlderThan(Animal a) {
return this.order < a.getOrder();
}
}
// ============================== Cat
static class Cat extends Animal {
public Cat(String n) {
super(n);
}
public String name() {
return "Cat: " + name;
}
}
static class Dog extends Animal {
public Dog(String n) {
super(n);
}
public String name() {
return "Dog: " + name;
}
}
// ============================== AnimalQueue
static class AnimalQueue {
LinkedList<Dog> dogs = new LinkedList<Dog>();
LinkedList<Cat> cats = new LinkedList<Cat>();
private int order = 0;
public void enqueue(Animal a) {
a.setOrder(order);
order++;
if (a instanceof Dog) {
dogs.addLast((Dog) a);
} else if (a instanceof Cat) {
cats.addLast((Cat)a);
}
}
public Animal dequeueAny() {
if (dogs.size() == 0) {
return dequeueCats();
} else if (cats.size() == 0) {
return dequeueDogs();
}
Dog dog = dogs.peek();
Cat cat = cats.peek();
if (dog.isOlderThan(cat)) {
return dogs.poll();
} else {
return cats.poll();
}
}
public Animal peek() {
if (dogs.size() == 0) {
return cats.peek();
} else if (cats.size() == 0) {
return dogs.peek();
}
Dog dog = dogs.peek();
Cat cat = cats.peek();
if (dog.isOlderThan(cat)) {
return dog;
} else {
return cat;
}
}
public int size() {
return dogs.size() + cats.size();
}
public Dog dequeueDogs() {
return dogs.poll();
}
public Dog peekDogs() {
return dogs.peek();
}
public Cat dequeueCats() {
return cats.poll();
}
public Cat peekCats() {
return cats.peek();
}
}
public static void main(String[] args) {
AnimalQueue animals = new AnimalQueue();
animals.enqueue(new Cat("Callie"));
animals.enqueue(new Cat("Kiki"));
animals.enqueue(new Dog("Fido"));
animals.enqueue(new Dog("Dora"));
animals.enqueue(new Cat("Kari"));
animals.enqueue(new Dog("Dexter"));
animals.enqueue(new Dog("Dobo"));
animals.enqueue(new Cat("Copa"));
System.out.println(animals.dequeueAny().name());
System.out.println(animals.dequeueAny().name());
System.out.println(animals.dequeueAny().name());
animals.enqueue(new Dog("Dapa"));
animals.enqueue(new Cat("Kilo"));
while (animals.size() != 0) {
System.out.println(animals.dequeueAny().name());
}
}
}
分享到:
相关推荐
Stack_Queue_Stack_源码
c++stack_和_queue用法,很好的介绍了STL中stack和queue的用法,及其使用方法
前端大厂最新面试题-stack_queue
常用的C++数据结构算法,包括队列、堆栈、链表...等.以模板类型式实现
Data structure: Stack->Queue Example
本源码实现了两个类,一个是带有最大值的栈和一个是带有最大值的队列。栈利用了两个C++的stack,队列利用了C++的queue。
实现栈,队列,链表的简单操作,其中链表包括单链表,双链表,循环链表。
Java_Stack_Queue Java中使用链表的堆栈和队列实现
Lists_Stack_Queue_PriorityQueue.java
stack_queue_p2pchat_practice:只是使用Stack和Queue技术的Java实践聊天应用程序
Pro_MERN_Stack_Full_Stack_Web_App_Development_with_Mongo,_Express,_React,_and_Node
TI公司提供2015年11月最新基于ZigBee无线智能家居开发协议(Z_Stack_Home_1.2.2a.exe) 由于只能上传小于60MB文件,将150MB原文件分割3个部分上传,分别为: Z_Stack_Home_1.2.2a.exe.part1.rar Z_Stack_Home_1.2.2a....
tcpip_stack_v1_2_TCP,IP_TCP_IP_udpmac_UDP_tcp.zip
NXP 的 LIN 协议栈,之前一直用的 4.5.7 的版本,使用过程中有好几个问题都是手动修复的。最近在官网上看见了 4.6.6 的版本,下载测试了一遍,发现之前手动修复的几个问题都已经 OK 了。
飞思卡尔官方最新LIN栈源码,可用于LIN应用开发参考和LIN的学习
面试Bit-Stack_and_queue 它由堆栈的解决方案和从面试位开始的基于队列的问题组成。
小猫钓鱼纸牌游戏。A和B两个同学玩简单的纸牌游戏,每人手里有n张牌,两人轮流出牌并依次排列在桌面上,每次出掉手里的第1张牌,出牌后如果发现桌面上有跟刚才打出的牌的数字相同的牌,则把到相同的那张牌结束的全部...
stack_queue
TI公司提供2015年11月最新基于ZigBee无线智能家居开发协议(Z_Stack_Home_1.2.2a.exe) 由于只能上传小于60MB文件,将150MB原文件分割3个部分上传,分别为: Z_Stack_Home_1.2.2a.exe.part1.rar Z_Stack_Home_1.2.2a....