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

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

    1 用户可以调用add方法将cat类或者dog类的实例放入队列中;

    2 用户可以调用pollAll方法,将队列中所有的实例按照队列的先后顺序依次弹出;

    3 用户可以调用pollDog方法,将队列中dog类的实例按照队列的先后顺序依次弹出;

    4 用户可以调用pollCat方法,将队列中cat类的实例按照队列的先后顺序依次弹出;

    5 用户可以调用isEmpty方法,检查队列中是否还有dog和cat的实例;

    6 用户可以调用isDogEmpty方法,检查队列中是否还有do的实例;

    7 用户可以调用isCatEmpty方法,检查队列中是否还有cat的实例

    public class CatAndDogQueue {
    
        private Queue<PetEntry> catQueue = new LinkedList<PetEntry>();
        private Queue<PetEntry> dogQueue = new LinkedList<PetEntry>();
        private long count = 0;
    
        public void add(Pet pet) {
            if (pet.getType().equals("Dog")) {
                this.dogQueue.add(new PetEntry(pet, this.count++));
            } else if (pet.getType().equals("Cat")) {
                this.catQueue.add(new PetEntry(pet, this.count++));
            } else {
                throw new RuntimeException("error! input not dog or cat!");
            }
    
        }
    
        public Pet popDog() {
            if (!dogQueue.isEmpty()) {
                return dogQueue.poll().getPet();
            } else {
                throw new RuntimeException("DogQueue is Empty");
            }
        }
    
        public Pet popCat() {
            if (!catQueue.isEmpty()) {
                return catQueue.poll().getPet();
            } else {
                throw new RuntimeException("DogQueue is Empty");
            }
        }
    
        public Pet popAll() {
            if (!catQueue.isEmpty() && !dogQueue.isEmpty()) {
                if (catQueue.peek().getCount() < dogQueue.peek().getCount()) {
                    return catQueue.poll().getPet();
                } else {
                    return dogQueue.poll().getPet();
                }
            } else if (!catQueue.isEmpty()) {
                return catQueue.poll().getPet();
            } else if (!dogQueue.isEmpty()) {
                return dogQueue.poll().getPet();
            } else {
                throw new RuntimeException("err! queue is empty");
            }
        }
    
        public boolean isEmpty() {
            return dogQueue.isEmpty() && catQueue.isEmpty();
        }
    
        public boolean isDogEmpty() {
            return dogQueue.isEmpty();
        }
    
        public boolean isCatEmpty() {
            return catQueue.isEmpty();
        }
    
    }
    
    class PetEntry {
        private Pet pet;
        private long count;
    
        public PetEntry(Pet pet, long count) {
            this.pet = pet;
            this.count = count;
        }
    
        public Pet getPet() {
            return this.pet;
        }
    
        public long getCount() {
            return this.count;
        }
    
        public String getEntryPetType() {
    
            return this.pet.getType();
        }
    }
    
    class Pet {
        private String type;
    
        public Pet(String type) {
            this.type = type;
        }
    
        public String getType() {
            return this.type;
        }
    }
    
    class Dog extends Pet {
        public Dog() {
            super("Dog");
        }
    }
    
    class Cat extends Pet {
        public Cat() {
            super("Cat");
        }
    }
  • 相关阅读:
    hibernate4 无法保存 不报错
    win10开启mongodb 开启服务
    nodejs学习笔记
    mariadb Too many connections
    新老ECS数据库速度对比
    数据库自动备份并打成tar.gz包
    字符串和数组----string
    I/O复用(select)——回声服务器端/客户端
    回声UDP服务器端/客户端
    回声TCP服务器端/客户端
  • 原文地址:https://www.cnblogs.com/moris5013/p/11627674.html
Copyright © 2011-2022 走看看