数据结构与算法(Java) 21:猫狗队列

题目 宠物、狗和猫的类如下:

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类的实例。

思路 设置两个队列分别存放猫和狗,设置一个计数器为每个猫狗对象印上时间戳,在pollAll方法中,通过比较时间戳的大小来实现依次弹出。

package algorithm.section3;

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

public class DogCatQueue {
    public static class Pet {
        private String type;

        public Pet(String type) {
            this.type = type;
        }

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

    public static class Dog extends Pet{

        public Dog() {
            super("dog");
        }
    }

    public static class Cat extends Pet{

        public Cat() {
            super("cat");
        }
    }

    public static class PetComeIn{
        private Pet pet;
        private long count;

        public PetComeIn(Pet pet, long count){
            this.pet = pet;
            this.count = count;
        }

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

        public long getCount(){
            return this.count;
        }

        public String getPetComeInType(){
            return this.pet.getPetType();
        }
    }

    public static class DogAndCatQueue{
        private Queue<PetComeIn> dog;
        private Queue<PetComeIn> cat;
        private long count;

        public DogAndCatQueue(){
            this.dog = new LinkedList<PetComeIn>();
            this.cat = new LinkedList<PetComeIn>();
            this.count = 0;
        }

        public void add(Pet pet){
            if (pet.getPetType().equals("dog")){
                this.dog.add(new PetComeIn(pet, this.count++));
            } else if (pet.getPetType().equals("cat")){
                this.cat.add(new PetComeIn(pet, this.count++));
            } else {
                throw new RuntimeException("error! not a dog or cat!");
            }
        }

        public Dog pollDog(){
            if (!this.isDogEmpty()){
                return (Dog) this.dog.poll().getPet();
            } else {
                throw new RuntimeException("Dog queue is empty!");
            }
        }

        public Cat pollCat(){
            if (!isCatEmpty()){
                return (Cat) this.cat.poll().getPet();
            } else {
                throw new RuntimeException("Cat queue is empty!");
            }
        }

        public Pet pollAll(){
            if (!this.dog.isEmpty() && !this.cat.isEmpty()){
                if (this.dog.peek().getCount() < this.cat.peek().getCount()){
                    return this.dog.poll().getPet();
                } else {
                    return this.cat.poll().getPet();
                }
            } else if (!this.dog.isEmpty()) {
                return this.dog.poll().getPet();
            } else if (!this.cat.isEmpty()) {
                return this.cat.poll().getPet();
            } else {
                throw new RuntimeException("No pet!");
            }
        }

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

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

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

    public static void main(String[] args){
        DogAndCatQueue test = new DogAndCatQueue();

        Pet one = new Dog();
        Pet two = new Cat();
        Pet three = new Cat();
        Pet four = new Dog();
        Pet five = new Dog();

        test.add(one);
        test.add(two);
        test.add(three);
        test.add(four);
        test.add(five);

        while (!test.isDogEmpty()){
            System.out.println(test.pollDog().getPetType());
        }

        while (!test.isCatEmpty()){
            System.out.println(test.pollCat().getPetType());
        }
    }
}
发布了149 篇原创文章 · 获赞 36 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/Dorothy_Xue/article/details/105646892
今日推荐