public class Queue { private int count;//element number of queue private int head;//index of header private int tail;//index of tail private int MAXSIZE = 10;//capacity private int space []; //Constructor public Queue() { count = 0; head = tail = -1; space = new int[MAXSIZE]; } //Inspector public boolean isEmpty() { return (count == 0); } public int front() { if(count == 0) return -1; return space[head]; } public int rear() { if(count == 0) return -1; return space[tail]; } public String toString() { String str = "["; int j = head; for(int i = 0; i < count; i++) { str = str+" "+space[j]; j = (j+1)%MAXSIZE; } return str + "]"; } //Modifier public void enqueue(int e) { if(count == MAXSIZE) { int [] space2 = new int[MAXSIZE*2]; int j = head; int k = 0; for(int i = 0; i < MAXSIZE; i++) { space2[k] = space[j]; j = (j+1)%MAXSIZE; k++; } space = space2; head = 0; tail = MAXSIZE-1; space[tail+1] = e; MAXSIZE = MAXSIZE*2; count++; tail++; } else{ space[(tail+1)%MAXSIZE] = e; count++; tail = (tail+1)%MAXSIZE; if(count == 1) head = tail; } } public boolean dequeue() { if(count == 0) return false; if(count == 1) { head = tail = -1; count = 0; return true; } head = (head+1)%MAXSIZE; count--; return true; } }
public class QueueTest { public static void main(String args[]) { Queue q = new Queue(); for(int i = 0; i < 11; i++) q.enqueue(i); System.out.println(q); q.dequeue(); q.enqueue(11); System.out.println(q); } }