队列

class Queue<T> {
  private int front;//队头
  private int rear;//队尾
  private int count;//队元素个数
  private int queueSize;//队长度
  private T[] object;

  public Queue() {
    this(10);
  }

  public Queue(int queueSize) {
    this.queueSize = queueSize;
    this.object = (T[]) new Object[queueSize];
    this.front = 0;
    this.rear = 0;
    this.count = 0;
  }

  public boolean isEmpty() {
    return count == 0;
  }

  public boolean isFull() {
    return count == queueSize;
  }

  public void push(T o) {
    if (this.isFull()) {
      throw new RuntimeException("队列是满的");
    }
    count++;
    object[rear] = o;
    rear = (rear + 1) % queueSize;
  }

  public T pop() {
    T result;
    if (this.isEmpty()) {
      throw new RuntimeException("队列是空的");
    }
    result = this.object[front];
    count--;
    front = (front + 1) % queueSize;
    return result;
  }

  public T peek() {
    if (this.isEmpty()) {
      throw new RuntimeException("队列是空的");
    }
    return this.object[front];
  }

  public int getCount() {
    return count;
  }

  public int getQueueSize() {
    return queueSize;
  }

  public int getFront() {
    return front;
  }

  public int getRear() {
    return rear;
  }

  public T[] getObject() {
    return object;
  }
}

posted @ 2015-04-14 22:44  win24  阅读(88)  评论(0编辑  收藏  举报