Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

What is the output of the following code? Recall that stacks and queues print in an [a, b, c] format from bottom/front to top/back. Stack

What is the output of the following code? Recall that stacks and queues print in an [a, b, c] format from bottom/front to top/back.

 
Stack s = new Stack();
Queue q = new LinkedList();
s.push("how");
s.push("are");
s.push("you");
while (!s.isEmpty()) {
 q.add(s.pop());
}
System.out.println(q);

2) Write the output produced when the following method is passed each of the following stacks:

public static void mystery1(Stack s) {
 Queue q = new LinkedList();
 while (!s.isEmpty()) {
 int n = s.pop();
 q.add(n);
 q.add(n);
 }
 while (!q.isEmpty()) {
 s.push(q.remove());
 }
 System.out.println(s);
}

[2, 6, 1] __________________
[42, -3, 4, 15, 9] _____________
[30, 20, 10, 60, 50, 40] _____________

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Understanding Databases Concepts And Practice

Authors: Suzanne W Dietrich

1st Edition

1119827949, 9781119827948

More Books

Students also viewed these Databases questions

Question

How has the competition changed within the last three years?

Answered: 1 week ago

Question

=+ a. How does this change affect the incentives for working?

Answered: 1 week ago