首页 > 其他 > 详细

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

时间:2014-03-03 04:08:47      阅读:415      评论:0      收藏:0      [点我收藏+]

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





Stack_Queue 猫狗队列问题 Cat dog queue @CareerCup,布布扣,bubuko.com

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

原文:http://blog.csdn.net/fightforyourdream/article/details/20276787

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!