Show the final contents of the stacks and the queue q after the following code is executed. Which value…
Question:
Transcribed Image Text:
Show the final contents of the stacks and the queue q after the following code is executed.
Which value is in the top of the stack? Which value is in the front of the queue?
Stacks = new Stack ();
Queue q = new Queue ();
s.push (“J”);
q. enqueue (“V”);
s.push (“A”);
q. enqueue (“A”);
String s1, s2;
for (int i = 0; i < 4; i++) {
s1 = (String) q. dequeue ();
s2 = (String) s.pop();
q. enqueue (s1+s2);
s.push (q. peek () );
}
System.out.println ("Stack: "+s);
System.out.println("Queue: "+q);
Expert Answer:
Answer rating: 100% (QA)
Let s go through the code step by step to determine the final contents of the stack s and the queue
View the full answer