/*
* Author: zhangjian268
* Created: 20074る11ら と 04:59:23
* Modified: 20074る11ら と 04:59:23
* 數組實現環形隊列
*/
import java.util. * ;
public class ArrayCircularQueue {
private int front = 0, rear = 0;
private Object[] queue;
public ArrayCircularQueue(int maxElements) {
queue = new Object[maxElements];
}
public void insert(Object o) {
int temp = rear;
rear = (rear + 1) % queue.length;
if (front == rear) {
rear = temp;
//throw new FullQueueException();
}
queue[rear] = o;
}
public boolean isEmpty() {
return front == rear;
}
public boolean isFull() {
return ((rear + 1) % queue.length) == front;
}
public Object remove() {
if (front == rear)
return 0;
//throw new EmptyQueueException();
front = (front + 1) % queue.length;
return queue[front];
}
public static void main(String args[]) {
ArrayCircularQueue Q = new ArrayCircularQueue(16);
Q.insert("aa");
Q.insert("bb");
Q.insert("cc");
System.out.println(Q.remove());
Q.insert("dd");
System.out.println(Q.remove());
System.out.println(Q.remove());
System.out.println(Q.remove());
System.out.println(Q.remove());
}
}
* Author: zhangjian268
* Created: 20074る11ら と 04:59:23
* Modified: 20074る11ら と 04:59:23
* 數組實現環形隊列
*/
import java.util. * ;
public class ArrayCircularQueue {
private int front = 0, rear = 0;
private Object[] queue;
public ArrayCircularQueue(int maxElements) {
queue = new Object[maxElements];
}
public void insert(Object o) {
int temp = rear;
rear = (rear + 1) % queue.length;
if (front == rear) {
rear = temp;
//throw new FullQueueException();
}
queue[rear] = o;
}
public boolean isEmpty() {
return front == rear;
}
public boolean isFull() {
return ((rear + 1) % queue.length) == front;
}
public Object remove() {
if (front == rear)
return 0;
//throw new EmptyQueueException();
front = (front + 1) % queue.length;
return queue[front];
}
public static void main(String args[]) {
ArrayCircularQueue Q = new ArrayCircularQueue(16);
Q.insert("aa");
Q.insert("bb");
Q.insert("cc");
System.out.println(Q.remove());
Q.insert("dd");
System.out.println(Q.remove());
System.out.println(Q.remove());
System.out.println(Q.remove());
System.out.println(Q.remove());
}
}