public class ArrayQueue{
private Array array;
public ArrayQueue(int capacity){
array = new Array(capacity);
}
public ArrayQueue(){
array = new Array();
}
public int getSize(){
return array.getSize();
}
public boolean isEmpty(){
return array.isEmpty();
}
public int getCapacity(){
return array.getCapacity();
}
public void enqueue(int e){
array.addLast(e);
}
public int dequeue(){
return array.removeFirst();
}
public int getFront(){
return array.getFirst();
}
}
public class LinkedListQueue{
private calss Node{
public Integer e;
public Node next;
public Node(Integer e, Node next){
this.e = e;
this.next = next;
}
public Node(Integer e){
this(e,null);
}
public Node(){
this(null,null);
}
}
private Node head;
private Node tail;
private int size;
public LinkedListQueue(){
head = null;
tail = null;
size = 0;
}
public int getSize(){
return size;
}
public boolean isEmpty(){
return size == 0;
}
public void enqueue(Integer e){
if(tail == null){
tail = new Node(e);
head = tail
}else{
tail.next = new Node(e);
tail = tail.next;
}
size++;
}
public Integer dequeue(){
if(isEmpty()){
throw new IllegalArgumentException("Cannot dequeue from an empty queue.");
}
Node retNode = head;
head = head.next;
retNode.next = null;
if(head == null){
tail = null;
}
size--;
return retNode.e;
}
}
public class LoopQueue{
private int[] data;
private int front;
private int tail;
private int size;
public LoopQueue(int capacity){
data = new int[capacity+1];
front = 0;
tail = 0;
size = 0
}
public LoopQueue(){
this(10);
}
public int getCapacity(){
return data.length - 1;
}
public boolean isEmpty(){
return front == tail;
}
public int getSize(){
return size;
}
public void enqueue(int e){
if((tail + 1)%data.length == front){
resize(getCapacity() * 2);
}
data[tail] = e;
tail = (tail + 1)%data.length;
size++;
}
public int dequeue(){
if(isEmpty()){
throw new IllegalArgumentException("Cannot dequeue from an empty queue");
}
int ret = data[front];
front = (front+1)%data.length;
size--;
if(size == getCapacity()/4 && getCapacity()/2 != 0){
resize(getCapacity()/2);
}
return ret;
}
public int getFront(){
if(isEmpty()){
throw new IllegalArgumentException("Cannot dequeue from an empty queue");
}
return data[front];
}
private void resize(int newCapacity){
int[] newData = new int[newCapacity+1];
for(int i = 0; i < size; i++){
newData[i] = data[(i + front)%data.length];
}
data = newData;
front = 0;
tail = size;
}
}