`
xitonga
  • 浏览: 587037 次
文章分类
社区版块
存档分类
最新评论

Stack_Queue 猫狗队列问题 Cat dog queue @CareerCup

 
阅读更多

An animal shelter holds only dogs and cats, and operates ona strictly “first in, first out” basis. People must adopt either the “oldest”(based on arrival time) of all animals at the shelter, or they can selectwhether they could prefer a dog or a cat (and will receive the oldest animal ofthat type). They cannot select which specific animal they would like. Createthe data structures to maintain this system and implement operations such asenqueue, dequeueAny, dequeueDog and dequeueCat. You may use the built-inLinkedList data structure.


大意是有一个动物收养站,猫或狗都不停地被收养进来,同时也有人想领养。规则是领养人可以挑选猫或狗,但不能挑具体哪一只,只能挑选早被收养的那只动物。


思路:

分别建猫和狗两个队列就搞定了。



package Stack_Queue;

import java.util.LinkedList;

public class S3_7 {

	// ============================== Animal
	static abstract class Animal {
		private int order; 
		protected String name;
		public Animal(String n) {
			name = n;
		}

		public abstract String name();

		public void setOrder(int ord) {
			order = ord;
		}

		public int getOrder() {
			return order;
		}

		public boolean isOlderThan(Animal a) {
			return this.order < a.getOrder();
		}
	}
	
	// ============================== Cat
	static class Cat extends Animal {
		public Cat(String n) {
			super(n);
		}

		public String name() {
			return "Cat: " + name;
		}
	}
	
	static class Dog extends Animal {
		public Dog(String n) {
			super(n);
		}

		public String name() {
			return "Dog: " + name;
		}
	}
	
	// ============================== AnimalQueue
	static class AnimalQueue {
		LinkedList<Dog> dogs = new LinkedList<Dog>();
		LinkedList<Cat> cats = new LinkedList<Cat>();
		private int order = 0;

		public void enqueue(Animal a) {
			a.setOrder(order);
			order++;
			if (a instanceof Dog) {
				dogs.addLast((Dog) a);
			} else if (a instanceof Cat) {
				cats.addLast((Cat)a);
			}
		}

		public Animal dequeueAny() {
			if (dogs.size() == 0) {
				return dequeueCats();
			} else if (cats.size() == 0) {
				return dequeueDogs();
			}
			Dog dog = dogs.peek();
			Cat cat = cats.peek();
			if (dog.isOlderThan(cat)) {
				return dogs.poll();
			} else {
				return cats.poll();
			}
		}

		public Animal peek() {
			if (dogs.size() == 0) {
				return cats.peek();
			} else if (cats.size() == 0) {
				return dogs.peek();
			}
			Dog dog = dogs.peek();
			Cat cat = cats.peek();
			if (dog.isOlderThan(cat)) {
				return dog;
			} else {
				return cat;
			}
		}

		public int size() {
			return dogs.size() + cats.size();
		}

		public Dog dequeueDogs() {
			return dogs.poll();
		}

		public Dog peekDogs() {
			return dogs.peek();
		}

		public Cat dequeueCats() {
			return cats.poll();
		}

		public Cat peekCats() {
			return cats.peek();
		}
	}
	
	
	public static void main(String[] args) {
		AnimalQueue animals = new AnimalQueue();
		animals.enqueue(new Cat("Callie"));
		animals.enqueue(new Cat("Kiki"));
		animals.enqueue(new Dog("Fido"));
		animals.enqueue(new Dog("Dora"));
		animals.enqueue(new Cat("Kari"));
		animals.enqueue(new Dog("Dexter"));
		animals.enqueue(new Dog("Dobo"));
		animals.enqueue(new Cat("Copa"));

		System.out.println(animals.dequeueAny().name());	
		System.out.println(animals.dequeueAny().name());	
		System.out.println(animals.dequeueAny().name());	

		animals.enqueue(new Dog("Dapa"));
		animals.enqueue(new Cat("Kilo"));

		while (animals.size() != 0) {
			System.out.println(animals.dequeueAny().name());	
		}
	}
}





分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics