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

Java: Given the output of the following program: public class StacksAndQueues {

ID: 3697558 • Letter: J

Question

Java:

Given the output of the following program:

public class StacksAndQueues

{

      Public static void main(String[] args)

      {

              Stack<integer> s = new Stack <integer>();

              Queue<integer> q = new Queue<integer>();

              q.enqueue(1);

              q.enqueue(2);

              q.enqueue(3);

              q.enqueue();

              s.push(q.dequeue());

              s.push(4);

              s.push(5);

              q.enqueue(6);

              q.enqueue(s.pop());

              q.enqueue(7);

             q.enqueue(q.dequeue());

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

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

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

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

             System.out.println(s.pop());

             System.out.println(s.pop());

}

}

Explanation / Answer

Hello there ,

Pleas find below code and it's output.

Let me know if you have any queries.

Thanks.

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class StacksAndQueues {
   public static void main(String[] args) {
       Stack<Integer> s = new Stack<Integer>();
       Queue<Integer> q = new LinkedList<Integer>();
       q.add(1);
       q.add(2);
       q.add(3);

       s.push(q.remove());
       s.push(4);
       s.push(5);
       q.add(6);
       q.add(s.pop());
       q.add(7);
       q.add(q.remove());
       System.out.println("Queue Stat" + q.toString());
       System.out.println("Stack Stat" + s.toString());
       System.out.println(q.remove());
       System.out.println(q.remove());
       System.out.println(q.remove());
       System.out.println(q.remove());
       System.out.println(s.pop());
       System.out.println(s.pop());
   }
}

===O/P===

Queue Stat[3, 6, 5, 7, 2]
Stack Stat[1, 4]
3
6
5
7
4
1

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