zoukankan      html  css  js  c++  java
  • 栈--猫狗队列

    题目

    public class Pet {
        private String type;
        public Pet(String type){
            this.type = type;
        }
        public String getPetType(){
            return this.type;
        }
    }
    public class Cat extends Pet{
        public Cat() {
            super("Cat");
        }
    }
    public class Dog extends Pet{
        public Dog(){
            super("Dog");
        }
    }
    

    要求:

    • add可以将dog类 cat类示例加入队列中
    • pollAll
    • pollDog
    • pollCat
    • isEmpty
    • isDogEmpty
    • isCatEmpty

    解答

    将不同示例盖上时间戳,但是又不修改原有类,定义一个新类:PetEnterQueue

    核心:添加计数器!!!

    public class PetEnterQueue {
        private Pet pet;
        private int count;
    
        public PetEnterQueue(Pet pet,int count){
            this.pet = pet;
            this.count = count;
        }
    
        public Pet getPet(){
            return this.pet;
        }
    
        public int getCount(){
            return this.count;
        }
    
        public String getEnterPetType(){
            return this.pet.getPetType();
        }
    
    }
    

    队列实现如下:

    public class DogCatQueue {
        private Queue<PetEnterQueue> dogQ;
        private Queue<PetEnterQueue> catQ;
        private int count;
        public DogCatQueue(){
            dogQ = new LinkedList<PetEnterQueue>();
            catQ = new LinkedList<PetEnterQueue>();
            this.count = 0;
        }
        public void add(Pet pet){
            if(pet.getPetType().equals("dog")){
                dogQ.add(new PetEnterQueue(pet,this.count++));
            }else if(pet.getPetType().equals("cat")){
                catQ.add(new PetEnterQueue(pet,this.count++));
            }else {
                throw new RuntimeException("error");
            }
        }
        public Pet pollAll(){
            if(!dogQ.isEmpty()&&!catQ.isEmpty()){
                if(dogQ.peek().getCount()<catQ.peek().getCount()){
                    return dogQ.poll().getPet();
                }else{
                    return catQ.poll().getPet();
                }
            }else if (!dogQ.isEmpty()){
                return dogQ.poll().getPet();
            }else if (!catQ.isEmpty()){
                return catQ.poll().getPet();
            }
            else {
                throw new RuntimeException("empty queue");
            }
        }
        public Dog pollDog(){
            if(!dogQ.isEmpty()){
                return (Dog) dogQ.poll().getPet();
            }else {
                throw new RuntimeException("empty dog");
            }
        }
        public Cat pollCat(){
            if(!catQ.isEmpty()){
                return (Cat) catQ.poll().getPet();
            }else {
                throw new RuntimeException("empty cat");
            }
        }
        public boolean isEmpty(){
            return dogQ.isEmpty()&&catQ.isEmpty();
        }
        public boolean isDogEmpty(){
            return dogQ.isEmpty();
        }
        public boolean isCatEmpty(){
            return catQ.isEmpty();
        }
    }
    
  • 相关阅读:
    spring-tool-suite-4-4.3.2.RELEASE-e4.12.0-win32.win32.x86_64 下载
    day39_Spring学习笔记_07_CRM_03
    MyEclipse 中 报错 ERROR PARSER:56
    day38_Spring学习笔记_06_CRM_02
    最简单的递归/死循环
    day37_Spring学习笔记_05_CRM_01
    如何在Linux中发现IP地址冲突
    如何在Linux中用命令行工具管理KVM虚拟环境
    使用 Shell 脚本自动化 Linux 系统维护任务
    Linux系统多网卡绑定实战
  • 原文地址:https://www.cnblogs.com/swifthao/p/12790681.html
Copyright © 2011-2022 走看看