Answer to Question #274644 in Java | JSP | JSF for mysti

Question #274644

Sample Output

After the Enqueue operations:

1. Element Front: 10

2. Element rear: 30

3. Elements in the Queue: 10 15 20 25 30

4. Number of Elements in the Queue: 5


The dequeued element is 10.

The dequeued element is 15.

After the Dequeue operations:

1. Element Front: 20

2. Element rear: 30

3. Elements in the Queue: 20 25 30

4. Number of Elements in the Queue: 3


COMPLETE THE CODE

package myqueue_J;

public class MyQueue_J {

public int data;

public MyQueue_J next;

public static MyQueue_J front=null;

public static MyQueue_J rear=null;

public static MyQueue_J temp=null;


public MyQueue_J(int d, MyQueue_J n) {

data=d;

next=n;

}

public static void enqueue(int d){

MyQueue_J temp = new MyQueue_J(d, null);

Insert to perform enqueue operation

}

public static void dequeue(){

Insert to perform dequeue operation

}

public static int printQueue(){

int ctr=0;


Insert to display the elements in the queue

}

public static void main(String[] args) {


Insert to perform the desired output

}

}


1
Expert's answer
2021-12-02T18:46:08-0500
class Element {
    private static int F, R, Capa;
    private static int queue[];


    Element(int c)
    {
        F = R = 0;
        Capa = c;
        queue = new int[Capa];
    }
    static void queueEnqueue(int data)
    {
       
        if (Capa == R) {
            System.out.printf("\nQueue is full\n");
            return;
        }
        else {
            queue[R] = data;
            R++;
        }
        return;
    }
    static void queueDequeue()
    {
        if (F == R) {
            System.out.printf("\nQueue is empty\n");
            return;
        }
        else {
            for (int i = 0; i < R - 1; i++) {
                queue[i] = queue[i + 1];
            }
            if (R < Capa)
                queue[R] = 0;
 
            
            R--;
        }
        return;
    }
 
  
    static void queueDisplay()
    {
        int i;
        if (F == R) {
            System.out.printf("\nQueue is Empty\n");
            return;
        }
        for (i = F; i < R; i++) {
            System.out.printf(" %d <-- ", queue[i]);
        }
        return;
    }
    static void queueFront()
    {
        if (F == R) {
            System.out.printf("\nQueue is Empty\n");
            return;
        }
        System.out.printf("\nFront Element is: %d", queue[F]);
        return;
    }
}
 
public class Main {
 
    public static void main(String[] args)
    {
        Element q = new Element(5);
        q.queueDisplay();
        q.queueEnqueue(10);
        q.queueEnqueue(15);
        q.queueEnqueue(20);
        q.queueEnqueue(25);
        q.queueEnqueue(30);
        
        q.queueDisplay();
        q.queueEnqueue(30);
        q.queueDisplay();
 
        q.queueDequeue();
        q.queueDequeue();
        System.out.printf("\n\nafter two node deletion\n\n");
        q.queueDisplay();
        q.queueFront();
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS