TESTS GIVE ERROR FOR SHRINK AND add remove 100 values ALSO, DO IT BY CHECKING THE MAIN CLASS WITHOUT CHANGING IT
WITE JAVA CODE JUST TO THE GIVEN CODE BELOW!
TESTS GIVE ERROR FOR SHRINK AND add remove 100 values
ALSO, DO IT BY CHECKING THE MAIN CLASS WITHOUT CHANGING IT
class Queue<T> {
private T[] a = (T[]) new Object[1];
private int N = 0;
public boolean isEmpty() {
return N == 0;
}
public int size() {
return N;
}
private void resize(int capacity) {
T[] temp = (T[]) new Object[capacity];
for (int i = 0; i < N; i++) {
temp[i] = a[i];
}
a = temp;
}
public void enqueue(T item) {
if (N == a.length) resize(2*a.length);
a[N++] = item;
}
public T dequeue() {
T item = a[--N];
a[N] = null;
if (N > 0 && N == a.length/4) resize(a.length/2);
return item;
}
// this method is for unit testing; don't touch
public int getArraySize(){ return a.length; }
}
public class Main {
public static void main(String[] args) {
Queue<Integer> myQueue = new Queue<Integer>();
int i;
i = 1;
myQueue.enqueue(i); System.out.printf("myQueue.enqueue(%d);\n",i);
i = 2;
myQueue.enqueue(i); System.out.printf("myQueue.enqueue(%d);\n",i);
i = 3;
myQueue.enqueue(i); System.out.printf("myQueue.enqueue(%d);\n",i);
i = 4;
myQueue.enqueue(i); System.out.printf("myQueue.enqueue(%d);\n",i);
i = 5;
myQueue.enqueue(i); System.out.printf("myQueue.enqueue(%d);\n",i);
int x = myQueue.dequeue();
System.out.printf("myQueue.dequeue() gives back %d;\n",x);
x = myQueue.dequeue();
System.out.printf("myQueue.dequeue() gives back %d;\n",x);
x = myQueue.dequeue();
System.out.printf("myQueue.dequeue() gives back %d;\n",x);
i = 6;
myQueue.enqueue(i); System.out.printf("myQueue.enqueue(%d);\n",i);
i = 7;
myQueue.enqueue(i); System.out.printf("myQueue.enqueue(%d);\n",i);
i = 8;
myQueue.enqueue(i); System.out.printf("myQueue.enqueue(%d);\n",i);
x = myQueue.dequeue();
System.out.printf("myQueue.dequeue() gives back %d;\n",x);
x = myQueue.dequeue();
System.out.printf("myQueue.dequeue() gives back %d;\n",x);
x = myQueue.dequeue();
System.out.printf("myQueue.dequeue() gives back %d;\n",x);
i = 9;
myQueue.enqueue(i); System.out.printf("myQueue.enqueue(%d);\n",i);
i = 10;
myQueue.enqueue(i); System.out.printf("myQueue.enqueue(%d);\n",i);
x = myQueue.dequeue();
System.out.printf("myQueue.dequeue() gives back %d;\n",x);
x = myQueue.dequeue();
System.out.printf("myQueue.dequeue() gives back %d;\n",x);
x = myQueue.dequeue();
System.out.printf("myQueue.dequeue() gives back %d;\n",x);
x = myQueue.dequeue();
System.out.printf("myQueue.dequeue() gives back %d;\n",x);
}
}
Step by step
Solved in 2 steps with 7 images