队列
AbstractQueue类
public abstract class AbstractQueue<E> { public abstract void push(E e); public abstract void pop(); public abstract E front(); }
用顺序表实现队列
public class ArrayQueue<E> extends AbstractQueue<E>{ private Object[] arr=new Object[4]; private int front=0,rear=0; @Override public void push(E e) { if((rear+1)%arr.length==front) throw new IndexOutOfBoundsException("超出队列长度!"); arr[rear]=e; rear=(rear+1)%arr.length; } @Override public void pop() { if(front==rear)throw new IllegalArgumentException("没有元素在队列中!"); arr[front]=null; front=(front+1)% arr.length; } @Override public E front() { if(front==rear)throw new IllegalArgumentException("没有元素在队列中!"); return (E) arr[front]; } }

浙公网安备 33010602011771号