【NowCoder】水题-[编程题]猫狗队列

【NowCoder】水题-[编程题]猫狗队列

题目

实现一种猫狗队列的结构,要求如下:

  1. 用户可以调用 add 方法将 cat 或者 dog 放入队列中
  2. 用户可以调用 pollAll 方法将队列中的 cat 和 dog 按照进队列的先后顺序依次弹出
  3. 用户可以调用 pollDog 方法将队列中的 dog 按照进队列的先后顺序依次弹出
  4. 用户可以调用 pollCat 方法将队列中的 cat 按照进队列的先后顺序依次弹出
  5. 用户可以调用 isEmpty 方法检查队列中是否还有 dog 或 cat
  6. 用户可以调用 isDogEmpty 方法检查队列中是否还有 dog
  7. 用户可以调用 isCatEmpty 方法检查队列中是否还有 cat

思路

  • 加入对象实例,需要添加对象的入队顺序
  • 弹出对象实例,也需要比对dog实例和cat实例的入队顺序

代码

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.SQLOutput;
import java.util.LinkedList;
import java.util.Queue;

class Dog{
    public String name;
    public int order;// 入队顺序

    // 初始化函数
    public Dog(String name,int order)
    {
        this.name = name;
        this.order = order;
    }
}

class Cat{
    public String name;
    public int order;// 入队顺序
    public Cat(String name,int order)
    {
        this.name = name;
        this.order = order;
    }
}

class CatDogQueue{
    public Queue<Cat> catQueue = new LinkedList<>();
    public Queue<Dog> dogQueue = new LinkedList<>();
    private int order = 0;// 入队顺序编号

    public CatDogQueue()
    {
        // 初始化函数
        catQueue = new LinkedList<>();
        dogQueue = new LinkedList<>();
    }

    // 入队的时候一定要添加入队顺序
    public void add(String animal,String id)
    {
        // 将cat类或者dog类的实例 放入队列中
        if(animal.equals("cat"))
        {
            // 将猫实例放入猫队列中
            catQueue.offer(new Cat(animal + " " + id,order));//
            order++;
        }

        if(animal.equals("dog"))
        {
            // 将狗实例  送入狗队列中
            dogQueue.offer(new Dog(animal + " " + id,order));
            order++;
        }
    }

    // 判断队列是否为空
    public String isEmpty()
    {
        // 猫狗队列都为空 才是空
        return catQueue.isEmpty() && dogQueue.isEmpty()?"yes":"no";
    }

    // 判断猫队列是否为空
    public String isCatEmpty()
    {
        return catQueue.isEmpty()?"yes":"no";
    }

    // 判断狗队列是个否为空
    public String isDogEmpty()
    {
        return dogQueue.isEmpty()?"yes":"no";
    }

    // 出队操作
    public String poll()
    {
        // 首先判断队列是否为空
        if(!catQueue.isEmpty() && !dogQueue.isEmpty())
        {
            // 比较一下猫队列和狗队列 队头元素的入队顺序  顺序小的先出队
            if(catQueue.peek().order < dogQueue.peek().order)
            {
                return pollCat();
            }
            else {
                return pollDog();
            }
        }
        else if(!catQueue.isEmpty())
        {
            return pollCat();
        }
        else if(!dogQueue.isEmpty())
        {
            return pollDog();
        }
        else {
            return "";
        }
    }

    // 猫出队
    public String pollCat(){
        return catQueue.poll().name;
    }

    // 狗出队
    public String pollDog(){
        return dogQueue.poll().name;
    }

}

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        CatDogQueue queue = new CatDogQueue();

        for(int i = 0; i < n; i++)
        {
            String[] params = br.readLine().trim().split(" ");// 按照空格分割
            if(params[0].equals("add"))
            {
                queue.add(params[1],params[2]);
            }
            else if(params[0].equals("pollAll"))
            {
                while(queue.isEmpty().equals("no"))
                {
                    System.out.println(queue.poll());
                }
            }
            else if(params[0].equals("pollCat"))
            {
                while(queue.isCatEmpty().equals("no"))
                {
                    System.out.println(queue.pollCat());
                }
            }
            else if(params[0].equals("pollDog"))
            {
                while(queue.isDogEmpty().equals("no"))
                {
                    System.out.println(queue.pollDog());
                }
            }
            else if(params[0].equals("isEmpty"))
            {
                System.out.println(queue.isEmpty());
            }
            else if(params[0].equals("isCatEmpty"))
            {
                System.out.println(queue.isCatEmpty());
            }
            else if(params[0].equals("isDogEmpty"))
            {
                System.out.println(queue.isDogEmpty());
            }
        }

    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

少写代码少看论文多多睡觉

求打赏,求关注,求点赞

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值