猫狗队列问题 -- 基本算法 包含作者分析过程

package basic_class_03;

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




/**
 * 猫狗队列 :
 * 用户可以调用add方法将cat类或dog类的 实例放入队列中; 
 * 用户可以调用pollAll方法,将队列中所有的实例按照进队列 的先后顺序依次弹出;
 * 用户可以调用pollDog方法,将队列中dog类的实例按照 进队列的先后顺序依次弹出; 
 * 用户可以调用pollCat方法,将队列中cat类的实 例按照进队列的先后顺序依次弹出;
 * 用户可以调用isEmpty方法,检查队列中是 否还有dog或cat的实例; 用户可以调用isDogEmpty方法,
 * 检查队列中是否有dog 类的实例; 用户可以调用isCatEmpty方法,检查队列中是否有cat类的实例
 * @author lenovo
 *
 */
public class Code_04_DogCatQueue_1 {


    /***
     * 本题的重点是猫跟够混在一起放在不的队列
     * 当从队列中拿时  是需要按照先后顺序的
     * @author lenovo
     *
     */

    public static class Pet {
        private String type;
        public Pet(String type) {
            this.type = type;
        }
        public String getType() {
            return this.type;
        }
    }

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

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


    /**
     * 需要注意的是   PetEnterQueue才是放入到猫狗队列中去的对象
     * 因为cat类跟dog类 只是实体类
     * 但是PetEnterQueue里面有count属性  是可以计数的
     * 需要利用到这个计数才能够实现猫跟狗的先进先出
     * @author lenovo
     *
     */
    public static 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.getType();
        }
    }


    public static 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.getType().equals("dog")) {
                this.dogQ.add(new PetEnterQueue(pet,this.count++));  // 每次放入一只狗  都需要记一次数  这个计数的变量是全局的
            } else if(pet.getType().equals("cat")) {
                this.catQ.add(new PetEnterQueue(pet,this.count++));  // 每次放入一只猫   都需要记一次数  这个计数的变量是全局的
            } else {
                throw new RuntimeException("err, not dog or cat");
            }
        }



        /**
         * 从队列中取猫
         * @return
         */
        public Cat pollCat() {
            if(!isCatQueueEmpty()) {
                return (Cat) this.catQ.poll().getPet();
            } else {
                throw new RuntimeException("Cat queue is empty");
            }
        }


        /**
         * 从队列中取狗
         * @return
         */
        public Dog pollDog() {
            if(!isDogQueueEmpty()) {
                return (Dog) this.dogQ.poll().getPet();
            } else {
                throw new RuntimeException("Dog Queue is empty");
            }
        }



        public Pet pollAll() {
            if(!this.dogQ.isEmpty() && !this.catQ.isEmpty()) {  // 猫跟狗队列都不为空
                // 注意这里使用的是  peek  而不是  poll   只要不是获取就不能使用  poll   是队列第一个元素
                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("err, queue is empty");
            }
        }


        // 判断队列是否为空
        public boolean isDogQueueEmpty() {
            return this.dogQ.isEmpty();
        }
        public boolean isCatQueueEmpty() {
            return this.catQ.isEmpty();
        }
    }



}

需要资源的请加2297581773 或者微信 18838927694

猜你喜欢

转载自blog.csdn.net/qq_38200548/article/details/81279524
今日推荐