100 猫狗队列
ac
三个双向链表
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
/**
* 猫狗队列
*/
class CatDogQueue{
LinkedList<Node> catQueue = new LinkedList<>();
LinkedList<Node> dogQueue = new LinkedList<>();
LinkedList<Node> totalQueue = new LinkedList<>();
public void pollDog() {
Node node = null;
while((node = dogQueue.pollFirst()) != null){
System.out.println(node.type.getStr() + " " + node.no);
totalQueue.remove(node);
}
}
public void pollCat() {
Node node = null;
while((node = catQueue.pollFirst()) != null){
System.out.println(node.type.getStr() + " " + node.no);
totalQueue.remove(node);
}
}
public void pollAll() {
dogQueue.clear();
catQueue.clear();
Node node = null;
while((node = totalQueue.pollFirst()) != null){
System.out.println(node.type.getStr() + " " + node.no);
}
}
public boolean isEmpty() {
return totalQueue.isEmpty();
}
public boolean isDogEmpty() {
return dogQueue.isEmpty();
}
public boolean isCatEmpty() {
return catQueue.isEmpty();
}
public void addDog(int no) {
Node animal = new Node(Animal.DOG, no);
dogQueue.addLast(animal);
totalQueue.addLast(animal);
}
public void addCat(int no) {
Node animal = new Node(Animal.CAT, no);
catQueue.addLast(animal);
totalQueue.addLast(animal);
}
static int unixTime = 0;
class Node{
public Animal type;
public int no;
public int time;
public Node(Animal type, int no) {
this.type = type;
this.no = no;
this.time = unixTime ++;
}
}
enum Animal{
DOG(0, "dog"),
CAT(1, "cat");
int type;
String str;
Animal(int type, String str) {
this.type = type;
this.str = str;
}
public String getStr() {
return str;
}
}
}
public class Main {
public static Scanner sc = new Scanner(System.in);
public static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws Exception {
String nStr = reader.readLine();
int n = Integer.valueOf(nStr);
CatDogQueue catDogQueue = new CatDogQueue();
for(int i=0;i<n;i++){
String s = reader.readLine();
if(s.equals("pollDog")){
catDogQueue.pollDog();
}else if(s.equals("pollCat")){
catDogQueue.pollCat();
}else if(s.equals("pollAll")){
catDogQueue.pollAll();
}else if(s.equals("isEmpty")){
if(catDogQueue.isEmpty()){
System.out.println("yes");
}else {
System.out.println("no");
}
}else if(s.equals("isDogEmpty")){
if(catDogQueue.isDogEmpty()){
System.out.println("yes");
}else {
System.out.println("no");
}
}else if(s.equals("isCatEmpty")) {
if(catDogQueue.isCatEmpty()){
System.out.println("yes");
}else {
System.out.println("no");
}
}else {
String[] arr = s.split(" ");
int animalNo = Integer.valueOf(arr[2]);
if(arr[1].equals("cat")){
catDogQueue.addCat(animalNo);
}else { // dog
catDogQueue.addDog(animalNo);
}
}
}
}
}
/*
11
add cat 1
add dog 2
pollAll
isEmpty
add cat 5
isDogEmpty
pollCat
add dog 10
add cat 199
pollDog
pollAll
cat 1
dog 2
yes
yes
cat 5
dog 10
cat 199
*/
一个队列(每次遍历,遍历删除)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
/**
* 猫狗队列
*/
class CatDogQueue{
LinkedList<Node> totalQueue = new LinkedList<>();
int catSize = 0;
int dogSize = 0;
public void printNode(Node node){
System.out.println(node.type.str + " " + node.no);
}
public void pollDog() {
List<Node> tmpList = new ArrayList<>();
for(Node node1: totalQueue){
if(node1.type.type == Animal.DOG.type){
printNode(node1);
tmpList.add(node1);
}
}
for(Node node1: tmpList) {
totalQueue.remove(node1);
}
dogSize = 0;
}
public void pollCat() {
List<Node> tmpList = new ArrayList<>();
for(Node node1: totalQueue){
if(node1.type.type == Animal.CAT.type){
printNode(node1);
tmpList.add(node1);
}
}
for(Node node1: tmpList) {
totalQueue.remove(node1);
}
catSize = 0;
}
public void pollAll() {
Node node = null;
while((node = totalQueue.pollFirst()) != null){
printNode(node);
}
catSize = 0;
dogSize = 0;
}
public boolean isEmpty() {
return totalQueue.isEmpty();
}
public boolean isDogEmpty() {
return dogSize == 0;
}
public boolean isCatEmpty() {
return catSize == 0;
}
public void addDog(int no) {
Node animal = new Node(Animal.DOG, no);
totalQueue.addLast(animal);
dogSize++;
}
public void addCat(int no) {
Node animal = new Node(Animal.CAT, no);
totalQueue.addLast(animal);
catSize++;
}
static int unixTime = 0;
class Node{
public Animal type;
public int no;
public int time;
public Node(Animal type, int no) {
this.type = type;
this.no = no;
this.time = unixTime ++;
}
}
enum Animal{
DOG(0, "dog"),
CAT(1, "cat");
public int type;
public String str;
Animal(int type, String str) {
this.type = type;
this.str = str;
}
}
}
public class Main {
public static Scanner sc = new Scanner(System.in);
public static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws Exception {
String nStr = reader.readLine();
int n = Integer.valueOf(nStr);
CatDogQueue catDogQueue = new CatDogQueue();
for(int i=0;i<n;i++){
String s = reader.readLine();
if(s.equals("pollDog")){
catDogQueue.pollDog();
}else if(s.equals("pollCat")){
catDogQueue.pollCat();
}else if(s.equals("pollAll")){
catDogQueue.pollAll();
}else if(s.equals("isEmpty")){
if(catDogQueue.isEmpty()){
System.out.println("yes");
}else {
System.out.println("no");
}
}else if(s.equals("isDogEmpty")){
if(catDogQueue.isDogEmpty()){
System.out.println("yes");
}else {
System.out.println("no");
}
}else if(s.equals("isCatEmpty")) {
if(catDogQueue.isCatEmpty()){
System.out.println("yes");
}else {
System.out.println("no");
}
}else {
String[] arr = s.split(" ");
int animalNo = Integer.valueOf(arr[2]);
if(arr[1].equals("cat")){
catDogQueue.addCat(animalNo);
}else { // dog
catDogQueue.addDog(animalNo);
}
}
}
}
}
/*
11
add cat 1
add dog 2
pollAll
isEmpty
add cat 5
isDogEmpty
pollCat
add dog 10
add cat 199
pollDog
pollAll
cat 1
dog 2
yes
yes
cat 5
dog 10
cat 199
*/
两个队列,节点加时间戳
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
class CatDogQueue{
LinkedList<Node> catQueue = new LinkedList<>();
LinkedList<Node> dogQueue = new LinkedList<>();
public void printNode(Node node){
System.out.println(node.type.str + " " + node.no);
}
public void pollDog() {
Node node = null;
while((node = dogQueue.pollFirst()) != null){
printNode(node);
}
}
public void pollCat() {
Node node = null;
while((node = catQueue.pollFirst()) != null){
printNode(node);
}
}
public void pollAll() {
while (!catQueue.isEmpty() && !dogQueue.isEmpty()){
Node dog = dogQueue.getFirst();
Node cat = catQueue.getFirst();
// 时间越小,表示进入队列的越早
if(dog.time < cat.time){
printNode(dog);
dogQueue.pollFirst();
}else {
printNode(cat);
catQueue.pollFirst();
}
}
if(!catQueue.isEmpty()){
pollCat();
}
if(!dogQueue.isEmpty()){
pollDog();
}
}
public boolean isEmpty() {
if(isCatEmpty() && isDogEmpty()){
return true;
}
return false;
}
public boolean isDogEmpty() {
return dogQueue.isEmpty();
}
public boolean isCatEmpty() {
return catQueue.isEmpty();
}
public void addDog(int no) {
Node animal = new Node(Animal.DOG, no);
dogQueue.addLast(animal);
}
public void addCat(int no) {
Node animal = new Node(Animal.CAT, no);
catQueue.addLast(animal);
}
static int unixTime = 0;
class Node{
public Animal type;
public int no;
public int time;
public Node(Animal type, int no) {
this.type = type;
this.no = no;
this.time = unixTime ++;
}
}
enum Animal{
DOG(0, "dog"),
CAT(1, "cat");
public int type;
public String str;
Animal(int type, String str) {
this.type = type;
this.str = str;
}
}
}
public class Main {
public static Scanner sc = new Scanner(System.in);
public static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws Exception {
String nStr = reader.readLine();
int n = Integer.valueOf(nStr);
CatDogQueue catDogQueue = new CatDogQueue();
for(int i=0;i<n;i++){
String s = reader.readLine();
if(s.equals("pollDog")){
catDogQueue.pollDog();
}else if(s.equals("pollCat")){
catDogQueue.pollCat();
}else if(s.equals("pollAll")){
catDogQueue.pollAll();
}else if(s.equals("isEmpty")){
if(catDogQueue.isEmpty()){
System.out.println("yes");
}else {
System.out.println("no");
}
}else if(s.equals("isDogEmpty")){
if(catDogQueue.isDogEmpty()){
System.out.println("yes");
}else {
System.out.println("no");
}
}else if(s.equals("isCatEmpty")) {
if(catDogQueue.isCatEmpty()){
System.out.println("yes");
}else {
System.out.println("no");
}
}else {
String[] arr = s.split(" ");
int animalNo = Integer.valueOf(arr[2]);
if(arr[1].equals("cat")){
catDogQueue.addCat(animalNo);
}else { // dog
catDogQueue.addDog(animalNo);
}
}
}
}
}
/*
11
add cat 1
add dog 2
pollAll
isEmpty
add cat 5
isDogEmpty
pollCat
add dog 10
add cat 199
pollDog
pollAll
cat 1
dog 2
yes
yes
cat 5
dog 10
cat 199
*/