Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

infiniteQueue should overwrite the engueue(Object) function as follows: If the q

ID: 3629348 • Letter: I

Question

infiniteQueue should overwrite the engueue(Object) function as follows:

If the queue is full and an enqueue operation is attempted, your new method should (1) in-

stantiate a new, larger queue, (2) copy all the elements of the previous queue to the new queue, and

(3) add the element that was passed to the method. Create a scenario where you demonstrate the correct

operation of infiniteQueue in the console output. Note there is also a simple Queue interface provided.

I have the class set up I just need the method. Thank you.

Explanation / Answer

This is the infiniteQueue class. The new enque method checks if isFull and if isFull it then checks if the front < rear. If the front is greater than rear it copies all items up to the rear and then adds a space so the front are all copied forward one position so you can fit in the new element. Otherwise it just copies directly to a tmp[] and then increases the array by 1 and copies back.

public class infiniteQueue extends finiteQueue{

    public infiniteQueue(int capacity) {
        super(capacity);
    }
    public void enqueue(Object item){
        if (isFull()){
            System.out.println("in if "+item);
            Object tmp[]= new Object[queue.length+1];
           
            for(int x =0;x<queue.length;x++){
                tmp[x]=queue[x];
            }
            queue=new Object[tmp.length];
            if (front<rear){
                for(int x =0;x<queue.length;x++){
                    if (x!=rear)
                        queue[x]=tmp[x];
                }
            } else {
                for(int x =0;x<front;x++){
                    if (x!=rear)
                        queue[x]=tmp[x];
                }
                for(int x =front;x<queue.length-1;x++){
                        queue[x+1]=tmp[x];
                }
                front=(front+1)%queue.length;
            }
        }
        queue[rear]=item;
        rear=(rear+1)%queue.length;
    }
    public boolean isFull(){
        return size()==queue.length-1;
    }
   
    public static void main(String args[]){
        infiniteQueue iq=new infiniteQueue(5);
        System.out.println("Initial size="+iq.size());
        iq.enqueue(1);
        iq.enqueue(2);
        iq.enqueue(3);
        iq.enqueue(4);
        iq.enqueue(5);
        for(Object o: iq.queue)
            System.out.print(o+" ");
        System.out.println();
        iq.enqueue(6);
        for(Object o: iq.queue)
            System.out.print(o+" ");
        System.out.println();
        System.out.println("After adding size="+iq.size());
        iq.dequeue();
        for(Object o: iq.queue)
            System.out.print(o+" ");
        System.out.println();
        System.out.println("After dequeue size="+iq.size());
        iq.enqueue(7);
        for(Object o: iq.queue)
            System.out.print(o+" ");
        System.out.println();
        iq.enqueue(8);
        for(Object o: iq.queue)
            System.out.print(o+" ");
        System.out.println();
        iq.enqueue(9);
        System.out.println("After adding size="+iq.size());
        for(Object o: iq.queue)
            System.out.print(o+" ");
        iq.enqueue(10);
        System.out.println("After adding size="+iq.size());
        for(Object o: iq.queue)
            System.out.print(o+" ");
    }
}