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

Given the following array based Queue class declaration, write the methods speci

ID: 3724781 • Letter: G

Question

Given the following array based Queue class declaration, write the methods specified. Both the enqueue and dequeue method must run in O(1) time. 2. . public void enqueue (E n) public E dequeue () public boolean isFu 11() public boolean isEmpty) public class QueuecE> private int maxSize: private int currentSize: private E [ storage: private int front, rear: public Queue (int size) ( maxsize = size; currentSize = 0; storage (E]) new Object [maxSizel: front = rear = 0; public void enqueue (E n) public E dequeue) public boolean isFull) public boolean isEmpty) f

Explanation / Answer

class Queue<E>

{

    private int maxSize;

    private int currentSize;

    private E[] storage;

    private int front, rear;

   

    // constructor

    public Queue(int size)

    {

        maxSize = size;

        currentSize = 0;

        storage = (E [])new Object[maxSize];

        front = rear = 0;

    }

   

    public void enqueue(E n)

    {

        if(isFull())

            System.out.println("Queue is Full!");

        else

        {

            // add element to queue

            storage[rear++] = n;

           

            // increase the size of queue

            currentSize++;

        }

    }

   

    public E dequeue()

    {

        if(isEmpty())

            return null;

        else

        {

            currentSize--;

           

            return storage[front++];

        }

    }

   

    public boolean isFull()

    {

        return currentSize == maxSize;

    }

   

    public boolean isEmpty()

    {

        return currentSize == 0;

    }

   

    public static void main(String[] args)

    {

        Queue<Integer> q = new Queue<Integer>(5);

       

        int i;

       

        for( i = 0 ; i <= 5 ; i++ )

            q.enqueue(i);

       

        while( !q.isEmpty() )

            System.out.println(q.dequeue());

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote