HOME/剑指Offer/

猫狗队列问题

Article Outline
TOC
Collection Outline

猫狗队列问题

实现一种狗猫队列的结构,要求如下:

  • 用户可以调用 add 方法将 cat 类或 dog 类的实例放入队列中;
  • 用户可以调用 pollAll 方法,将队列中所有的实例按照进队列的先后顺序依次弹出;
  • 用户可以调用 pollDog 方法,将队列中 dog 类的实例按照进队列的先后顺序依次弹出;
  • 用户可以调用 pollCat 方法,将队列中 cat 类的实例按照进队列的先后顺序依次弹出;
  • 用户可以调用 isEmpty 方法,检查队列中是否还有 dog 或 cat 的实例;
  • 用户可以调用 isDogEmpty 方法,检查队列中是否有 dog 类的实例;
  • 用户可以调用 isCatEmpty 方法,检查队列中是否有 cat 类的实例。

解答: 分别建立狗队列和猫队列,然后使用时间戳(这里可以使用一个 count 变量代替),然后只要进入一个动物类就 count + 1,然后将该值封装到该对象中;依次类推

package cn.test;

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

/**
 * @Author smallmartial
 * @Date 2020/3/4
 * @Email [email protected]
 */
public class CatDogQueue {
    public static class Pet{
        private String type;
        public Pet(String type) {
            this.type = type;
        }

        public  String getType() {
            return this.type;
        }
    }

    public static class DogPet extends Pet{
        public DogPet() {
            super("dog");
        }
    }

    public static class CatPet extends Pet{
        public CatPet() {
            super("cat");
        }
    }

    /**
     * Pet enter
     */
    public static class PetEnter{

        private Pet pet;

        private Integer count;

        public PetEnter(Pet pet, Integer count) {
            this.pet = pet;
            this.count = count;
        }

        public String getEnterPetType() {
            return this.pet.getType();
        }

        public Pet getPet() {
            return this.pet;
        }

        public Integer getCount() {
            return count;
        }
    }

    public static class DogAndCatQueue{
        private Queue<PetEnter> dog;
        private Queue<PetEnter> cat;
        private Integer count;
        public DogAndCatQueue() {
            dog = new LinkedList<PetEnter>();
            cat = new LinkedList<PetEnter>();
            count = 0;
        }

        public void add(Pet pet){
            if(pet.getType().equals("dog")){
                dog.add(new PetEnter(pet,count++));
            }else if(pet.getType().equals("cat")){
                cat.add(new PetEnter(pet,count++));
            }else {
                throw new RuntimeException("err not cat or dog");
            }
        }

        public Pet pollAll(){
            if(!dog.isEmpty() && !cat.isEmpty()){
                if (dog.peek().count < cat.peek().count){
                    return dog.poll().getPet();
                }else {
                    return cat.poll().getPet();
                }
            }else if(!dog.isEmpty()){
                return dog.poll().getPet();
            }else if(!cat.isEmpty()){
                return cat.poll().getPet();
            }else {
                throw new RuntimeException("err, queue is empty!");
            }
        }

        public Pet pollDog(){
            if(!dog.isEmpty()){
                return dog.poll().getPet();
            }else {
                throw new RuntimeException("err,dog queue is empty");
            }
        }

        public Pet pollCat(){
            if(!cat.isEmpty()){
                return cat.poll().getPet();
            }else {
                throw new RuntimeException("err,cat queue is empty");
            }
        }

        public boolean isEmpty() {
            return this.dog.isEmpty() && this.cat.isEmpty();
        }

        public boolean isDogQueueEmpty() {
            return this.dog.isEmpty();
        }

        public boolean isCatQueueEmpty() {
            return this.cat.isEmpty();
        }
        public static void main(String[] args) {
            DogAndCatQueue test = new DogAndCatQueue();

            Pet dog1 = new DogPet();
            Pet cat1 = new CatPet();
            Pet dog2 = new DogPet();
            Pet cat2 = new CatPet();
            Pet dog3 = new DogPet();
            Pet cat3 = new CatPet();

            test.add(dog1);
            test.add(cat1);
            test.add(dog2);
            test.add(cat2);
            test.add(dog3);
            test.add(cat3);

            test.add(dog1);
            test.add(cat1);
            test.add(dog2);
            test.add(cat2);
            test.add(dog3);
            test.add(cat3);

            while (!test.isDogQueueEmpty()) {
                System.out.println(test.pollDog().getType());
            }
            while (!test.isEmpty()) {
                System.out.println(test.pollAll().getType());
            }
        }
    }
}