一种消息接收并打印的结构设计

这里写图片描述

import java.util.HashMap;
//一种消息接收并打印的结构设计
public class PrintStruct{

      //定义节点的信息
      public static class Node{

           public int num;  //定义节点的值
           public  Node next; 

           public Node(int num){
               this.num=num;
           }
      }

      public static class MessageBox{

      private  HashMap<Integer,Node>headMap;
      private  HashMap<Integer,Node>tailMap;
      private  int lastPrint;


      //构造函数实现成员的初始化
      public MessageBox()
      {
            headMap=new HashMap<Integer,Node>();
            tailMap=new HashMap<Integer,Node>();
            lastPrint=0;
      }
      //接收数字
      public  void receive(int num){

              if(num<1)
              {
                  return ;
              }
              Node cur=new Node(num);
              headMap.put(num,cur);
              tailMap.put(num,cur);
              if(tailMap.containsKey(num-1))
              {
                 tailMap.get(num-1).next=cur;  //链接到当前节点
                 tailMap.remove(num-1);
                 headMap.remove(num);
              }
              if(headMap.containsKey(num+1))
              {
                 cur.next=headMap.get(num+1);
                 tailMap.remove(num);
                 headMap.remove(num+1);
              }
              if(headMap.containsKey(lastPrint+1))
              {
                 print();  //打印输出所有的数
              }
      }

      //打印输出数
      public void print()
      {
            Node node=headMap.get(++lastPrint);
            headMap.remove(lastPrint);
            while(node!=null)
            {
                 System.out.print(node.num+" ");
                 node=node.next;
                 lastPrint++;
            }
            tailMap.remove(--lastPrint);
            System.out.println();

      }
      }

       //产生随机的1-10之间的数
      public static int  generateRan()
      { 
              return (int)(Math.random()*10);
      } 
      public static  void main(String[]args)
      { 
          MessageBox box=new MessageBox();
         //System.out.println("Hello");
        // for(int i=0;i<100;i++)
        // {
        //   int k=generateRan();
        //   System.out.println(k);
        //   box.receive(k);
        // }

        box.receive(2); // - 2
        box.receive(1); // 1 2 -> print, trigger is 1

        box.receive(4); // - 4
        box.receive(5); // - 4 5
        box.receive(7); // - 4 5 - 7
        box.receive(8); // - 4 5 - 7 8
        box.receive(6); // - 4 5 6 7 8
        box.receive(3); // 3 4 5 6 7 8 -> print, trigger is 3

        box.receive(9); // 9 -> print, trigger is 9

        box.receive(10); // 10 -> print, trigger is 10

        box.receive(12); // - 12
        box.receive(13); // - 12 13
        box.receive(11); // 11 12 13 -> print, trigger is 11

      }
}

这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值