标签:
private T[]queue; privatestaticint size =20; //队列容量 privateint front, rear; //队首、队尾下标
publicArrayQueue(){ queue=(T[])newObject[size]; front =0; rear =0; }
publicvoid add(T t) throws Exception { if((rear +1)% size == front) thrownewException("over flow!"); rear =(rear +1)% size; queue[rear]= t; }
public T poll() throws Exception { if(front == rear) thrownewException("under flow!"); front =(front +1)% size; returnqueue[front]; }
public boolean isEmpty(){ return front == rear; }
//结点类 publicclassNode{ public T data; publicNode next; publicNode(T obj,Node next){ this.data = obj; this.next = next; } } privateNode head,front,rear;
publicLinkQueue(){ head =newNode(null,null); front = rear = head; size =0; }
//从队尾入队 publicvoid add(T t) { Node s =newNode(t,null); rear.next = s; rear = s; size++;//队列长度+1 }
//从队头出队 public T poll() throws Exception { if(rear == front) thrownewException("under flow!"); Node temp = front.next; //暂存队首,以便返回 front.next = front.next.next; if(front.next == null) //最后一个元素出队:还要对队尾处理 rear = front; return temp.data; }
标签:
原文地址:http://www.cnblogs.com/Doing-what-I-love/p/5445067.html