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

Could this be shown in Java, I would appreciate it. You are given the code skele

ID: 3589221 • Letter: C

Question


Could this be shown in Java, I would appreciate it.

You are given the code skeleton for implementing a queue using Stack (that is in turn implemented using a Doubly Linked List) Complete the code for the enqueue0, dequeue() and isEmpty0 functions of the Queue class. The code for the main function is also given to you. After you implement the above three functions, you can run your main function and capture screenshot. The queue size can range from 5 to 10 and the maximum value for an element in the queue can be 50 As you can notice, there are two Stacks (stackl and stack2) declared as private member variables in the Queue class. You need to use these two Stacks for implementing the functionalities of a queue. I suggest thelfollowing design (you are free to choose your own design; provide a detailed explanation in your project report if your design is different from mine) Use stackl to store the elements of the queue (with the invariant that the topmost element of stackl is the element at the front of the queue and the bottommost element of stackl is the element at the end of the queue) and use stack2 as an auxiliary data structure to implement the enqueue function. Since the topmost element of stackl is the element in the front of the queue, the dequeue function can be simply implemented as the result of a pop operation on stackl. It is the enqueue function that needs to be thought out in greater detail and implemented. I suggest the following idea for enqueue of an integer 'data First check if stackl is empty or not. If it is empty, simply push the 'data' to it and return from the enqueue function. If stackl is not empty to start with, then pop out all the elements of stackl and push each of them to stack2. After stackl gets empty, push the data' to it. Now, pop out all the elements of stack2 and push them back to stackl. As a result of this, the newly enqueued 'data' will be in the bottom of stackl

Explanation / Answer

import java.util.Stack;

public class GFG

{

    /* class of queue having two stacks */

    static class Queue

    {

        Stack<Integer> stack1 ;

        Stack<Integer> stack2 ;

    }

     

    /* Function to push an item to stack*/

    static void push(Stack<Integer> top_ref, int new_data)

    {

        //Push the data onto the stack

        top_ref.push(new_data);

    }

    /* Function to pop an item from stack*/

    static int pop(Stack<Integer> top_ref)

    {

        /*If stack is empty then error */

        if(top_ref.isEmpty())

        {

            System.out.println("Stack Overflow");

            System.exit(0);   

        }

        //pop the data from the stack

        return top_ref.pop();

    }

    //Function to enqueue an item to the queue

    static void enQueue(Queue q, int x)

    {

        push(q.stack1, x);

    }

    /* Function to dequeue an item from queue */

    static int deQueue(Queue q)

    {

        int x;

        /* If both stacks are empty then error */

        if(q.stack1.isEmpty() && q.stack2.isEmpty() )

        {

            System.out.println("Q is empty");

            System.exit(0);

        }

        /* Move elements from stack1 to stack 2 only if

        stack2 is empty */

        if(q.stack2.isEmpty())

        {

            while(!q.stack1.isEmpty())

            {

            x = pop(q.stack1);

            push(q.stack2, x);   

            }

        }

        x = pop(q.stack2);

        return x;

    }

    /* Driver function to test anove functions */

    public static void main(String args[])

    {

        /* Create a queue with items 1 2 3*/

        Queue q= new Queue();

        q.stack1 = new Stack<>();

        q.stack2 = new Stack<>();

        enQueue(q, 1);

        enQueue(q, 2);

        enQueue(q, 3);

        /* Dequeue items */

        System.out.print(deQueue(q)+" ");

        System.out.print(deQueue(q)+" ");

        System.out.println(deQueue(q)+" ");

    }

}

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