描述
实现一种猫狗队列的结构,要求如下:
1. 用户可以调用 add 方法将 cat 或者 dog 放入队列中
2. 用户可以调用 pollAll 方法将队列中的 cat 和 dog 按照进队列的先后顺序依次弹出
3. 用户可以调用 pollDog 方法将队列中的 dog 按照进队列的先后顺序依次弹出
4. 用户可以调用 pollCat 方法将队列中的 cat 按照进队列的先后顺序依次弹出
5. 用户可以调用 isEmpty 方法检查队列中是否还有 dog 或 cat
6. 用户可以调用 isDogEmpty 方法检查队列中是否还有 dog
7. 用户可以调用 isCatEmpty 方法检查队列中是否还有 cat
输入描述:
第一行输入一个整数 n 表示 用户的操作总次数。
以下 n行 每行表示用户的一次操作
每行的第一个参数为一个字符串 s,若 s = “add”, 则后面接着有 “cat x”(表示猫)或者“dog x”(表示狗),其中的 x 表示猫狗的编号。
输出描述:
对于每个操作:
若为 “add”,则不需要输出。
以下仅列举几个代表操作,其它类似的操作输出同理。
若为 “pollAll”,则将队列中的 cat 和 dog 按照进队列的先后顺序依次弹出。(FIFO),格式见样例。
若为 "isEmpty",则检查队列中是否还有 dog 或 cat, 为空则输出 “yes”, 否则输出 “no”。
示例1
输入:
11
add cat 1
add dog 2
pollAll
isEmpty
add cat 5
isDogEmpty
pollCat
add dog 10
add cat 199
pollDog
pollAll
输出:
cat 1
dog 2
yes
yes
cat 5
dog 10
cat 199
import java.util.Queue;
import java.util.LinkedList;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
DogCatQueue dogCatQueue = new DogCatQueue();
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(in.readLine());
StringBuilder res = new StringBuilder();
Pet pet = null;
while (n-->0){
String[] strArr = in.readLine().split(" ");
String ops = strArr[0];
switch (ops){
case "add":
if ("dog".equals(strArr[1])){
dogCatQueue.add(new Dog(Integer.parseInt(strArr[2])));
} else if ("cat".equals(strArr[1])){
dogCatQueue.add(new Cat(Integer.parseInt(strArr[2])));
} else {
throw new RuntimeException("Invalid add.");
}
break;
case "pollAll":
while (!dogCatQueue.isEmpty()){
pet = dogCatQueue.pollAll();
res.append(pet.getPetType()).append(" ").append(pet.getX()).append('\n');
}
break;
case "pollDog":
while (!dogCatQueue.isDogQueueEmpty()){
pet = dogCatQueue.pollDog();
res.append("dog").append(" ").append(pet.getX()).append('\n');
}
break;
case "pollCat":
while (!dogCatQueue.isCatQueueEmpty()){
pet = dogCatQueue.pollCat();
res.append("cat").append(" ").append(pet.getX()).append('\n');
}
break;
case "isEmpty":
if (!dogCatQueue.isEmpty()){
res.append("no").append('\n');
} else {
res.append("yes").append('\n');
}
break;
case "isDogEmpty":
if(!dogCatQueue.isDogQueueEmpty()){
res.append("no").append('\n');
} else {
res.append("yes").append('\n');
}
break;
case "isCatEmpty":
if(!dogCatQueue.isCatQueueEmpty()){
res.append("no").append('\n');
} else {
res.append("yes").append('\n');
}
break;
}
}
System.out.print(res);
in.close();
}
}
//pet模板类 抽象类
class Pet{
private String type;
private int x;
public Pet(String type,int x){
this.type=type;
this.x=x;
}
public String getPetType(){
return this.type;
}
public int getX(){
return this.x;
}
}
//实体类dog
class Dog extends Pet{
public Dog(int x){
super("dog",x);
}
}
//实体类cat
class Cat extends Pet{
public Cat(int x){
super("cat",x);
}
}
//新定义一个队列,存有pet和 新增的时间戳count 用来记录前后顺序
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.getPetType();
}
}
//猫狗队列
class DogCatQueue{
private Queue<PetEnterQueue> dogQ;
private Queue<PetEnterQueue> catQ;
private long count;
public DogCatQueue(){
this.dogQ=new LinkedList<PetEnterQueue>();//
this.catQ=new LinkedList<PetEnterQueue>();
this.count=0;
}
public void add(Pet pet){
if(pet.getPetType().equals("dog")){
this.dogQ.add(new PetEnterQueue(pet,this.count++));
}else if(pet.getPetType().equals("cat")){
this.catQ.add(new PetEnterQueue(pet,this.count++));
}else{
throw new RuntimeException("错了!不是狗也不是猫!");
}
}
public Pet pollAll(){
if(!this.dogQ.isEmpty()&&!this.catQ.isEmpty()){
if(this.dogQ.peek().getCount()<this.catQ.peek().getCount()){
return this.dogQ.poll().getPet();
}else{
return this.catQ.poll().getPet();
}
}else if(!this.dogQ.isEmpty()){
return this.dogQ.poll().getPet();
}else if(!this.catQ.isEmpty()){
return this.catQ.poll().getPet();
}else{
throw new RuntimeException("错了!你的队列啥也没有!");
}
}
public Dog pollDog(){
if(!this.isDogQueueEmpty()){
return (Dog) this.dogQ.poll().getPet();
}else{
throw new RuntimeException("错了!你的队列啥也没有!");
}
}
public Cat pollCat(){
if(!this.isCatQueueEmpty()){
return (Cat) this.catQ.poll().getPet();
}else{
throw new RuntimeException("错了!你的队列啥也没有!");
}
}
public boolean isEmpty(){
return this.dogQ.isEmpty() && this.catQ.isEmpty();
}
public boolean isDogQueueEmpty(){
return this.dogQ.isEmpty();
}
public boolean isCatQueueEmpty(){
return this.catQ.isEmpty();
}
}