左神算法——第04题——狗猫队列

实现一种狗猫队列的结构,要求如下: 用户可以调用add方法将cat类或dog类的实例放入队列中; 用户可以调用pollAll方法,将队列中所有的实例按照进队列的先后顺序依次弹出;用户可以调用pollDog方法,将队列中dog类的实例按照进队列的先后顺序依次弹出; 用户可以调用pollCat方法,将队列中cat类的实 例按照进队列的先后顺序依次弹出; 用户可以调用isEmpty方法,检查队列中是否还有dog或cat的实例; 用户可以调用isDogEmpty方法,检查队列中是否有dog 类的实例;用户可以调用isCatEmpty方法,检查队列中是否有cat类的实例。

import java.util.LinkedList;
import java.util.Queue;

public class e06DogCatQueue {
    public static class Pet{
        private String type;
        private String name;
        public Pet(String type,String name){
            this.type=type;
            this.name=name;
        }
        public String getPetType() {
            return type;
        }

        @Override
        public String toString() {
            return "Pet{" +
                    "type='" + type + '\'' +
                    ", name='" + name + '\'' +
                    '}';
        }
    }
    public static class Dog extends Pet{
        public Dog(String name){
            super("dog",name);
        }
    }
    public static class Cat extends Pet{
        public Cat(String name){
            super("cat",name);
        }
    }
    public static class PetEnter{
        private Pet pet;
        private long count;
        public PetEnter(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();
        }
    }

    public static class DogCatQueue{
        private Queue<PetEnter> dogQ;
        private Queue<PetEnter> catQ;
        private long count;
        public DogCatQueue(){
            this.dogQ=new LinkedList<>();
            this.catQ=new LinkedList<>();
            this.count=0;
        }
        public void add(Pet pet){
            if (pet.getPetType().equals("dog")){
                dogQ.add(new PetEnter(pet,this.count++));
            }else if (pet.getPetType().equals("cat")){
                catQ.add(new PetEnter(pet,this.count++));
            }else{
                throw new RuntimeException("not a cat or not a dog");
            }
        }
        public Cat pollCat(){
            if (!catQ.isEmpty()){
                return (Cat)catQ.poll().getPet();
            }else{
                throw new RuntimeException("cat empty");
            }
        }
        public Dog pollDog(){
            if (!dogQ.isEmpty()){
                return (Dog)dogQ.poll().getPet();
            }else{
                throw new RuntimeException("dog empty");
            }
        }
        public Pet pollAll(){
            if (!catQ.isEmpty()&&!dogQ.isEmpty()){
                if (catQ.peek().getCount()<dogQ.peek().getCount()){
                    return catQ.poll().getPet();
                }else{
                    return dogQ.poll().getPet();
                }
            }else if (!catQ.isEmpty()){
                return catQ.poll().getPet();
            }else if(!dogQ.isEmpty()){
                return dogQ.poll().getPet();
            }else{
                throw new RuntimeException("all empty");
            }
        }
        public boolean isEmpty(){
            return this.catQ.isEmpty()&&this.dogQ.isEmpty();
        }
        public boolean isDogQueueEmpty(){
            return this.dogQ.isEmpty();
        }
        public boolean isCatQueueEmpty(){
            return this.catQ.isEmpty();
        }
    }

    public static void main(String[] args) {
        Cat c1=new Cat("大花");
        Cat c2=new Cat("二花");
        Cat c3=new Cat("三花");
        Dog d1=new Dog("大黑");
        Dog d2=new Dog("二黑");
        Dog d3=new Dog("三黑");
        DogCatQueue dogCatQueue=new DogCatQueue();
        dogCatQueue.add(c2);
        dogCatQueue.add(d2);
        dogCatQueue.add(c1);
        dogCatQueue.add(d1);
        dogCatQueue.add(d3);
        dogCatQueue.add(c3);
        System.out.println(dogCatQueue.pollDog());
        System.out.println(dogCatQueue.pollCat());
        System.out.println(dogCatQueue.pollAll());
        System.out.println(dogCatQueue.pollAll());
        System.out.println(dogCatQueue.pollAll());
        System.out.println(dogCatQueue.pollAll());
        System.out.println(dogCatQueue.pollAll());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值