肚脐开心 2021-11-05 15:12 采纳率: 72.2%
浏览 95
已结题

数据结构 栈和队列及递归算法

用Java语言:
使用3个队列,分别保留手机上最近10个,未接来电、已接来电、已拨电话

  • 写回答

2条回答 默认 最新

  • 钰娘娘 2021-11-08 16:16
    关注
    
    public class Main {
        public static void main(String[] args) {
            Main main = new Main();
    
            Op op = new Op();
            Scanner sc = new Scanner(System.in);
            int type;
            while((type=sc.nextInt())!=Op.OP_FINISH){
                String mobile = "";
                if (type < 4) {
                    mobile = sc.next();
                }
    
                op.operator(type,mobile);
            }
        }
    
        static class Op{
            Memory tel;
            Memory notAccept;
            Memory accept ;
            public static final int OP_TEL = 1;
            public static final int OP_REFUSE = 2;
            public static final int OP_ACCEPT = 3;
            public static final int OP_TEL_PRINT = 4;
            public static final int OP_REFUSE_PRINT = 5;
            public static final int OP_ACCEPT_PRINT = 6;
            public static final int OP_FINISH = 7;
            public Op(){
                tel = new Memory();
                notAccept = new Memory();
                accept = new Memory();
            }
    
            public void operator(int type,String mobile){
                switch (type){
                    case OP_TEL:
                        addTel(mobile);
                        break;
                    case OP_REFUSE:
                        addNotAccept(mobile);
                        break;
                    case OP_ACCEPT:
                        addAccept(mobile);
                        break;
                    case OP_TEL_PRINT:
                        System.out.println(getTelList());
                        break;
                    case OP_REFUSE_PRINT:
                        System.out.println(getNotAcceptList());
                        break;
                    case OP_ACCEPT_PRINT:
                        System.out.println(getAcceptList());
                        break;
    
                }
            }
    
            public void addTel(String mobile){
                tel.add(mobile);
            }
    
            public Queue<String> getTelList(){
                return tel.getList();
            }
    
            public void addNotAccept(String mobile){
                notAccept.add(mobile);
            }
    
            public Queue<String> getNotAcceptList(){
                return notAccept.getList();
            }
    
            public void addAccept(String mobile){
                accept.add(mobile);
            }
    
            public Queue<String> getAcceptList(){
                return accept.getList();
            }
        }
    
    
    
        static class Memory{
            Queue<String> list = null;//拨出
    
            public Memory(){
                list = new LinkedList<>();
    
            }
    
            public void add(String mobile){
                list.offer(mobile);
                if(list.size()>10)
                    list.poll();
            }
    
            public Queue<String> getList(){
                return list;
            }
    
    
    
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月10日
  • 已采纳回答 11月2日
  • 创建了问题 11月5日

悬赏问题

  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测