博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
左神算法书籍《程序员代码面试指南》——1_04猫狗队列
阅读量:4541 次
发布时间:2019-06-08

本文共 5267 字,大约阅读时间需要 17 分钟。

【题目】

宠物、狗和猫的类如下:

public class Pet {
private String type;
public Pet(String type) { this.type = type; }
public String getPetType() { return this.type; }
}
public class Dog extends Pet { public Dog() { super("dog"); } }
public class Cat extends Pet { public Cat() { super("cat"); } }
实现一种狗猫队列的结构,要求如下: 用户可以调用add方法将cat类或dog类的
实例放入队列中; 用户可以调用pollAll方法,将队列中所有的实例按照进队列
的先后顺序依次弹出; 用户可以调用pollDog方法,将队列中dog类的实例按照
进队列的先后顺序依次弹出; 用户可以调用pollCat方法,将队列中cat类的实
例按照进队列的先后顺序依次弹出; 用户可以调用isEmpty方法,检查队列中是
否还有dog或cat的实例; 用户可以调用isDogEmpty方法,检查队列中是否有dog
类的实例; 用户可以调用isCatEmpty方法,检查队列中是否有cat类的实例。

【题解】

使用类包含类的方法,且使用三个队列,分别存放总的类、cat类、dog类

【代码】

1 package chapter_1_stackandqueue;  2   3 import java.util.LinkedList;  4 import java.util.Queue;  5   6 public class Problem_04_DogCatQueue {  7   8     public static class Pet {  9         private String type; 10  11         public Pet(String type) { 12             this.type = type; 13         } 14  15         public String getPetType() { 16             return this.type; 17         } 18     } 19  20     public static class Dog extends Pet { 21         public Dog() { 22             super("dog"); 23         } 24     } 25  26     public static class Cat extends Pet { 27         public Cat() { 28             super("cat"); 29         } 30     } 31  32     public static class PetEnterQueue { 33         private Pet pet; 34         private long count; 35  36         public PetEnterQueue(Pet pet, long count) { 37             this.pet = pet; 38             this.count = count; 39         } 40  41         public Pet getPet() { 42             return this.pet; 43         } 44  45         public long getCount() { 46             return this.count; 47         } 48  49         public String getEnterPetType() { 50             return this.pet.getPetType(); 51         } 52     } 53  54     public static class DogCatQueue { 55         private Queue
dogQ; 56 private Queue
catQ; 57 private long count; 58 59 public DogCatQueue() { 60 this.dogQ = new LinkedList
(); 61 this.catQ = new LinkedList
(); 62 this.count = 0; 63 } 64 65 public void add(Pet pet) { 66 if (pet.getPetType().equals("dog")) { 67 this.dogQ.add(new PetEnterQueue(pet, this.count++)); 68 } else if (pet.getPetType().equals("cat")) { 69 this.catQ.add(new PetEnterQueue(pet, this.count++)); 70 } else { 71 throw new RuntimeException("err, not dog or cat"); 72 } 73 } 74 75 public Pet pollAll() { 76 if (!this.dogQ.isEmpty() && !this.catQ.isEmpty()) { 77 if (this.dogQ.peek().getCount() < this.catQ.peek().getCount()) { 78 return this.dogQ.poll().getPet(); 79 } else { 80 return this.catQ.poll().getPet(); 81 } 82 } else if (!this.dogQ.isEmpty()) { 83 return this.dogQ.poll().getPet(); 84 } else if (!this.catQ.isEmpty()) { 85 return this.catQ.poll().getPet(); 86 } else { 87 throw new RuntimeException("err, queue is empty!"); 88 } 89 } 90 91 public Dog pollDog() { 92 if (!this.isDogQueueEmpty()) { 93 return (Dog) this.dogQ.poll().getPet(); 94 } else { 95 throw new RuntimeException("Dog queue is empty!"); 96 } 97 } 98 99 public Cat pollCat() {100 if (!this.isCatQueueEmpty()) {101 return (Cat) this.catQ.poll().getPet();102 } else103 throw new RuntimeException("Cat queue is empty!");104 }105 106 public boolean isEmpty() {107 return this.dogQ.isEmpty() && this.catQ.isEmpty();108 }109 110 public boolean isDogQueueEmpty() {111 return this.dogQ.isEmpty();112 }113 114 public boolean isCatQueueEmpty() {115 return this.catQ.isEmpty();116 }117 118 }119 120 public static void main(String[] args) {121 DogCatQueue test = new DogCatQueue();122 123 Pet dog1 = new Dog();124 Pet cat1 = new Cat();125 Pet dog2 = new Dog();126 Pet cat2 = new Cat();127 Pet dog3 = new Dog();128 Pet cat3 = new Cat();129 130 test.add(dog1);131 test.add(cat1);132 test.add(dog2);133 test.add(cat2);134 test.add(dog3);135 test.add(cat3);136 137 test.add(dog1);138 test.add(cat1);139 test.add(dog2);140 test.add(cat2);141 test.add(dog3);142 test.add(cat3);143 144 test.add(dog1);145 test.add(cat1);146 test.add(dog2);147 test.add(cat2);148 test.add(dog3);149 test.add(cat3);150 while (!test.isDogQueueEmpty()) {151 System.out.println(test.pollDog().getPetType());152 }153 while (!test.isEmpty()) {154 System.out.println(test.pollAll().getPetType());155 }156 }157 158 }

 

转载于:https://www.cnblogs.com/zzw1024/p/11170465.html

你可能感兴趣的文章
C#使用CDO发送邮件
查看>>
Oracle中的NVL函数
查看>>
Redis Sentinel机制与用法说明【转】
查看>>
使用NUget发布自己的dll(转)
查看>>
7bit ASCII编解码
查看>>
flask-sqlalchemy(包含离线脚本,with在上下文管理的应用)
查看>>
机器学习工程师 - Udacity 强化学习 Part Ten
查看>>
go语言 新手学习笔记 go基础教程
查看>>
zabbix 添加宏变量
查看>>
中文词频统计
查看>>
Mark一个按照权重生成随机数方法
查看>>
Eclipse中SVN版本信息不显示的问题
查看>>
2016年11月1日——jQuery源码学习笔记
查看>>
[BZOJ]2806: [Ctsc2012]Cheat
查看>>
http报文
查看>>
前端学习记录
查看>>
hdoj 1541 stars(树状数组)
查看>>
引用:WebAPI中的定时处理-使用Quartz.Net
查看>>
电脑中毒 U盘所以文件被隐藏且不可设为可见
查看>>
Thinkphp5笔记二:创建模块
查看>>