队列

610阅读 0评论2015-05-03 woxin317
分类:Java



点击(此处)折叠或打开

  1. /*
  2.  * 列队类
  3.  */
  4. public class MyQueue {
  5.     //底层使用数组
  6.     private long[] arr;
  7.     //有效数据的大小
  8.     private int elements;
  9.     //队头
  10.     private int front;
  11.     //队尾
  12.     private int end;
  13.     
  14.     /**
  15.      * 默认构造方法
  16.      */
  17.     public MyQueue() {
  18.         arr = new long[10];
  19.         elements = 0;
  20.         front = 0;
  21.         end = -1;
  22.     }
  23.     
  24.     /**
  25.      * 带参数的构造方法,参数为数组的大小
  26.      */
  27.     public MyQueue(int maxsize) {
  28.         arr = new long[maxsize];
  29.         elements = 0;
  30.         front = 0;
  31.         end = -1;
  32.     }
  33.     
  34.     /**
  35.      * 添加数据,从队尾插入
  36.      */
  37.     public void insert(long value) {
  38.         arr[++end] = value;
  39.         elements++;
  40.     }
  41.     
  42.     /**
  43.      * 删除数据,从队头删除
  44.      */
  45.     public long remove() {
  46.         elements--;
  47.         return arr[front++];
  48.     }
  49.     
  50.     /**
  51.      * 查看数据,从队头查看
  52.      */
  53.     public long peek() {
  54.         return arr[front];
  55.     }
  56.     
  57.     /**
  58.      * 判断是否为空
  59.      */
  60.     public boolean isEmpty() {
  61.         return elements == 0;
  62.     }
  63.     
  64.     /**
  65.      * 判断是否满了
  66.      */
  67.     public boolean isFull() {
  68.         return elements == arr.length;
  69.     }
  70. }

上一篇:
下一篇:定时器的几种常用用法