public class Queue<E> { private int front; private int rear; private int count; private int queueSize; private E[] object; public Queue() { this(10); } public Queue(int queueSize) { this.queueSize = queueSize; this.object = (E[]) 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(E o) { if (this.isFull()) { throw new RuntimeException("队列是满的"); } count++; object[rear] = o; rear = (rear + 1) % queueSize; } public E pop() { E result; if (this.isEmpty()) { throw new RuntimeException("队列是空的"); } result = this.object[front]; count--; front = (front + 1) % queueSize; return result; } public E 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 E[] getObject() { return object; } }
java自定义队列
最新推荐文章于 2022-05-04 19:27:49 发布