Question: Given the following Stack and Queue operations: Stack public void push(int value) public int pop() public boolean isEmpty() Queue public void enqueue(int value) public int

Given the following Stack and Queue operations:

Stack

 public void push(int value) public int pop() public boolean isEmpty() 

Queue

 public void enqueue(int value) public int dequeue() public boolean isEmpty() 

Implement the following method: Queue reverseQueue(Queue queue) Returns a new queue that is the result of reversing the contents of the given queue (e.g. 1,2,3 becomes 3,2,1)

You are ONLY allowed to use Stack and Queue objects (no other data structures aside from simple primitives) but you can create as many Stacks and Queues as you need. You must return a Queue whose contents is the reverse of that stored in the given queue. You are to assume that the only operations available for you to use are those public ones listed above.

About edge cases: If the given Queue object is null, you should return null. If the given Queue object is not null but is empty, you should return an empty Queue (i.e. instantiated but not filled with anything). Otherwise, the given queue will have at least one item in it. You should handle all these cases.

public class Main { // entry point  given to show how we would call reverseQueue public static void main(String[] args) { // special operation added to produce a random queue Queue someQ = Queue.randomQueue(); // call with a null object Queue revQ1 = reverseQueue(null); // call with an empty queue Queue revQ2 = reverseQueue(new Queue()); // call with a non-empty queue (makes a copy of the queue first) Queue revQ3 = reverseQueue(someQ.copy()); // print the results to check our work System.out.println("Null Queue: " + revQ1); System.out.println("Empty Queue: " + revQ2); System.out.println("Non-empty Queue: \tOriginal: " + someQ + " \tReversed: " + revQ3); } private static Queue reverseQueue(Queue queue) { // write your code below

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!