猫狗队列题目

匿名 (未验证) 提交于 2019-12-03 00:34:01

源码下载链接:https://github.com/huijuanl/AlgorithmLearning.git

Ŀ¼: ./src/main/java/CatDogPackage/

实现一种猫狗队列的结构,要求如下:
用户可以调用add方法将cat类或者dog类的实例放入队列中;
用户可以调用poll方法,将队列中所有的实例按照队列的先后顺序依次弹出;
用户可以调用pollDog方法,将队列中dog类的实例按照队列的先后顺序依次弹出;
用户可以调用pollCat方法,将队列中cat类的实例按照队列的先后顺序依次弹出;
用户可以调用isEmpty方法,检查队列中是否还有dog和cat的实例;

package CatDogPackage;  import java.util.LinkedList; import java.util.Queue;  //已经有的类:Pet,Dog,Cat class Pet {     private String type;      public Pet(String type) {         this.type = type;     }      public String getPetType() {         return this.type;     }  }  class Dog extends Pet {     public Dog() {         super("Dog");     } }  class Cat extends Pet {     public Cat() {         super("Cat");     } }  //为解决猫狗队列问题新加的类:PetEnterQueue class PetEnterQueue {     Pet pet;     int count;      public PetEnterQueue(Pet pet, int count) {         this.pet = pet;         this.count = count;      }      public Pet getPet() {         return this.pet;     }      public int getCount() {         return this.count;     }  }  public class DogCatQueue {      Queue<PetEnterQueue> DogQueue;     Queue<PetEnterQueue> CatQueue;     int count;      public DogCatQueue() {         DogQueue = new LinkedList<PetEnterQueue>();         CatQueue = new LinkedList<PetEnterQueue>();         count = 0;     }      public void add(Pet pet) {         if (pet.getPetType().equals("Dog"))             this.DogQueue.add(new PetEnterQueue(pet, count++));         else if (pet.getPetType().equals("Cat"))             this.CatQueue.add(new PetEnterQueue(pet, count++));         else throw new RuntimeException("error:not Dog or Cat");      }      public boolean isEmpty() {         return this.DogQueue.isEmpty() && this.CatQueue.isEmpty();      }      public boolean isDogQueueEmpty() {         return this.DogQueue.isEmpty();     }      public boolean isCatQueueEmpty() {         return this.DogQueue.isEmpty();     }      public Pet poll() {         if (!this.DogQueue.isEmpty() && !this.CatQueue.isEmpty()) {             if (this.DogQueue.peek().getCount() < this.CatQueue.peek().getCount())                 return this.DogQueue.poll().getPet();             else return this.CatQueue.poll().getPet();          } else {             if (!this.DogQueue.isEmpty())                 return this.DogQueue.poll().getPet();             else if (!this.CatQueue.isEmpty())                 return this.CatQueue.poll().getPet();             else throw new RuntimeException("error:DogCatQueue is empty");         }       }      public Dog pollDog() {         if (!this.DogQueue.isEmpty())             return (Dog) this.DogQueue.poll().getPet();//强制转换         else throw new RuntimeException("error:DogQueue is empty");      }      public Cat pollCat() {         if (!this.CatQueue.isEmpty())             return (Cat) this.CatQueue.poll().getPet();//强制转换         else throw new RuntimeException("error:CatQueue is empty");      }   }

测试:

package CatDogPackage;  import org.junit.Test;  import static org.junit.Assert.*;  public class DogCatQueueTest {     @Test     public static void main(String[] args) {         DogCatQueue test = new DogCatQueue();          Pet dog1 = new Dog();         Pet cat1 = new Cat();         Pet dog2 = new Dog();         Pet cat2 = new Cat();         Pet dog3 = new Dog();         Pet cat3 = new Cat();          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().getPetType()); //        }         while (!test.isEmpty()) {             System.out.println(test.poll().getPetType());         }     }  }

文章来源: 猫狗队列题目
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!