貓狗隊(duì)列

問題:

給定數(shù)據(jù)結(jié)構(gòu).png
題目要求.png

思路:

在給定數(shù)據(jù)結(jié)構(gòu)的基礎(chǔ)上,自定義一個(gè)數(shù)據(jù)結(jié)構(gòu),包裝給定的數(shù)據(jù)結(jié)構(gòu),再加一個(gè)count計(jì)數(shù),不管加入的是狗還是貓,count都++,輸出整個(gè)隊(duì)列的時(shí)候,只需要比較貓狗隊(duì)列的隊(duì)列頂count的大小,小的先輸出。

代碼:

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 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.getPetType();
        }
    }

    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.getPetType().equals("dog")) {
                this.dogQ.add(new PetEnterQueue(pet, this.count++));
            } else if (pet.getPetType().equals("cat")) {
                this.catQ.add(new PetEnterQueue(pet, this.count++));
            } else {
                throw new RuntimeException("err, not dog or cat");
            }
        }

        public Pet pollAll() {
            if (!this.dogQ.isEmpty() && !this.catQ.isEmpty()) {
                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 Dog pollDog() {
            if (!this.isDogQueueEmpty()) {
                return (Dog) this.dogQ.poll().getPet();
            } else {
                throw new RuntimeException("Dog queue is empty!");
            }
        }

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

        public boolean isEmpty() {
            return this.dogQ.isEmpty() && this.catQ.isEmpty();
        }

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

        public boolean isCatQueueEmpty() {
            return this.catQ.isEmpty();
        }

    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,659評論 19 139
  • 本題來自程序員代碼面試指南 實(shí)現(xiàn)一種狗貓隊(duì)列的結(jié)構(gòu),要求如下: ●用戶可以調(diào)用add方法將cat類或dog類的實(shí)例...
    624c95384278閱讀 716評論 0 1
  • GCD調(diào)度隊(duì)列是執(zhí)行任務(wù)的強(qiáng)大工具。調(diào)度隊(duì)列允許您相對于調(diào)度者異步或者同步的執(zhí)行任意代碼塊。您能夠使用調(diào)度隊(duì)列來執(zhí)...
    坤坤同學(xué)閱讀 6,748評論 1 3
  • 機(jī)會(huì)來臨的時(shí)候,才不管你是男生還是女生,也不管你是25,還是30,或者35。它永遠(yuǎn)只會(huì)眷顧那些擁有的多,付出的多,...
    M月閱讀 416評論 0 0
  • 夏,心生煩躁,卻又溢出濃濃的綠意。 正如我如此渴望相聚,卻又害怕相聚,兒伴有些久別,愿是你念我,恰如我...
    kadyc閱讀 268評論 0 1

友情鏈接更多精彩內(nèi)容