肚脐开心 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 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助