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

Describe MyQueue class that uses an array to implement an integer queue supporti

ID: 3606908 • Letter: D

Question

Describe MyQueue class that uses an array to implement an integer queue supporting dequeue, enqueue, tail and retrieveing the minimum element.

Note that initial capacity of queue is 16.

import java.util.Arrays;

public class TestMyQueue {

public static void main(String args[]){

MyQueue queue = new MyQueue();

//ToDo

for(int i=1;i<=5; i++){

queue.enqueue(i);

}

for(int i=1;i<=5; i++){

queue.enqueue(i);

}

for(int i=20;i>=10; i--){

queue.enqueue(i);

}

System.out.println("Tail of the queue: "+queue.tail()); // 10

System.out.println("Min of the queue: "+queue.getMin()); // 1

System.out.println("Dequeue: "+queue.dequeue()); // 1

System.out.println("Dequeue: "+queue.dequeue()); // 2

System.out.println("Dequeue: "+queue.dequeue()); // 3

System.out.println("Min of the queue: "+queue.getMin()); //1

System.out.println("Dequeue: "+queue.dequeue()); // 4

System.out.println("Dequeue: "+queue.dequeue()); // 5

System.out.println("Dequeue: "+queue.dequeue()); // 1

System.out.println("Dequeue: "+queue.dequeue()); // 2

System.out.println("Dequeue: "+queue.dequeue()); // 3

System.out.println("Min of the queue: "+queue.getMin()); //4

System.out.println("Tail of the queue: "+queue.tail()); // 10

}

}

Explanation / Answer

Please find my implementation:

//A class to represent a queue

public class MyQueue

{

   int front, rear, size;

   int capacity;

   int array[];

   public MyQueue(int capacity) {

       this.capacity = capacity;

       front = this.size = 0;

       rear = capacity - 1;

       array = new int[this.capacity];

   }

   // Queue is full when size becomes equal to

   // the capacity

   boolean isFull()

   { return (size == capacity);

   }

   // Queue is empty when size is 0

   boolean isEmpty()

   { return (size == 0); }

   // Method to add an item to the queue.

   // It changes rear and size

   int enqueue( int item)

   {

       if (isFull())

           return -1;

       this.rear = (this.rear + 1)%this.capacity;

       this.array[this.rear] = item;

       this.size = this.size + 1;

       return item;

   }

   // Method to remove an item from queue.

   // It changes front and size

   int dequeue()

   {

       if (isEmpty())

           return Integer.MIN_VALUE;

       int item = this.array[this.front];

       this.front = (this.front + 1)%this.capacity;

       this.size = this.size - 1;

       return item;

   }

   // Method to get front of queue

   int front()

   {

       if (isEmpty())

           return Integer.MIN_VALUE;

       return this.array[this.front];

   }

   // Method to get rear of queue

   int tail()

   {

       if (isEmpty())

           return Integer.MIN_VALUE;

       return this.array[this.rear];

   }

  

   int getMin() {

       if(isEmpty())

           return Integer.MAX_VALUE;

       int min = array[front];

       for(int i=front; i !=rear; i = (i + 1)%this.capacity) {

           if(min > array[i])

               min = array[i];

          

       }

      

       return min;

      

   }

}

############

public class TestMyQueue {

   public static void main(String args[]){

       MyQueue queue = new MyQueue(16);

       //ToDo

       for(int i=1;i<=5; i++){

           queue.enqueue(i);

       }

       for(int i=1;i<=5; i++){

           queue.enqueue(i);

       }

       for(int i=20;i>=10; i--){

           queue.enqueue(i);

       }

       System.out.println("Tail of the queue: "+queue.tail()); // 10

       System.out.println("Min of the queue: "+queue.getMin()); // 1

       System.out.println("Dequeue: "+queue.dequeue()); // 1

       System.out.println("Dequeue: "+queue.dequeue()); // 2

       System.out.println("Dequeue: "+queue.dequeue()); // 3

       System.out.println("Min of the queue: "+queue.getMin()); //1

       System.out.println("Dequeue: "+queue.dequeue()); // 4

       System.out.println("Dequeue: "+queue.dequeue()); // 5

       System.out.println("Dequeue: "+queue.dequeue()); // 1

       System.out.println("Dequeue: "+queue.dequeue()); // 2

       System.out.println("Dequeue: "+queue.dequeue()); // 3

       System.out.println("Min of the queue: "+queue.getMin()); //4

       System.out.println("Tail of the queue: "+queue.tail()); // 10

   }

}

/*

Sample run:

Tail of the queue: 15

Min of the queue: 1

Dequeue: 1

Dequeue: 2

Dequeue: 3

Min of the queue: 1

Dequeue: 4

Dequeue: 5

Dequeue: 1

Dequeue: 2

Dequeue: 3

Min of the queue: 4

Tail of the queue: 15

*/

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