Question

Write the following method in java: public static void reverse(Queue<Integer> q) which uses a stack to...

Write the following method in java:

public static void reverse(Queue<Integer> q)

which uses a stack to reverse the order of all the items in q.

Homework Answers

Answer #1

The idea here is as to reverse the queue we need a data structure that temporarily stores the queue in reverse order. And as we know we can achieve this with a stack as that is LIFO. So, we one by one push all the elements in the stack till queue is not empty. The last element in the queue will be at the top of the stack. Then we again pop the elements from the stack and store it in the queue. steps :-

1. queue - [2,3,5,4]

2. stack - [4,5,3,2] (Pop the elements from the queue and insert into the stack. 4 is at the top)

queue - []

3. queue - [4,5,3,2] (Pop the elements of the stack to insert back into the queue.)

function

public static void reverse(Queue<Integer> q)

{

Stack<Integer> stack = new Stack<>();

        while (!q.isEmpty()) {

            stack.add(q.peek());

            q.remove();

        }

        while (!stack.isEmpty()) {

            q.add(stack.peek());

            stack.pop();

        }

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Using a queue and a stack, implement a method that tests whether a sequence of numbers...
Using a queue and a stack, implement a method that tests whether a sequence of numbers is a palindrome, that is, equal to the sequence in reverse. For example, [1,2,3,2,1] is a palindrome, but [1,2,2,3,1] is not. Complete the following file: QueueStackUtil.java import java.util.LinkedList; import java.util.Queue; import java.util.Stack; public class QueueStackUtil { public static boolean isPalindrome(int[] values) { Queue<Integer> queue = new LinkedList<Integer>(); Stack<Integer> stack = new Stack<Integer>(); for (int s : values) { // insert s into the queue...
Queues, Lists, and Stacks Assignment: Write client methods to do the following Assume that there exists...
Queues, Lists, and Stacks Assignment: Write client methods to do the following Assume that there exists in main a list   declared as : List<Integer> aList   =   new   List<Integer>(50); Write a client method called bubbleSort which given a list as a parameter uses the bubble sort algorithm to sort the contents of the list. YOU MAY ASSUME that there exists a method called swap (with the following signature) which swaps the contents of two elements: // swap public static void swap(List<Integer>...
THIS IS A JAVA PROGRAM THAT NEEDS TO BE WRITTEN Write a recursive method void reverse(ArrayList<Object>...
THIS IS A JAVA PROGRAM THAT NEEDS TO BE WRITTEN Write a recursive method void reverse(ArrayList<Object> obj) that reverses an ArrayList of any type of object. For example, if an ArrayList held 4 strings: "hi", "hello", "howdy", and "greetings" the order would become "greetings", "howdy", "hello", and "hi". Implement a recursive solution by removing the first object, reversing the ArrayList consisting of the remaining Objects, and combining the two. Use the following class ArrayListReverser to write and test your program....
Code in Java Create a queue class to store integers and implement following methods: 1) void...
Code in Java Create a queue class to store integers and implement following methods: 1) void enqueue(int num): This method will add an integer to the queue (end of the queue). 2) int dequeue(): This method will return the first item in the queue (First In First Out). 3) void display(): This method will display all items in the queue (First item will be displayed first). 4) Boolean isEmpty(): This method will check the queue and if it is empty,...
ex3 Write a method public static boolean isPalindrome(String input) that uses one or more stacks to...
ex3 Write a method public static boolean isPalindrome(String input) that uses one or more stacks to determine if a given string is a palindrome. [A palindrome is a string that reads the same forwards and backwards, for example ‘racecar’, ‘civic’]. Make sure that your method works correctly for special cases, if any. What is the big-O complexity of your method in terms of the list size n. Supplementary Exercise for Programming (Coding) [Stacks] Download and unpack (unzip) the file Stacks.rar....
JAVA: Write a method with the following header to return a string format to represent the...
JAVA: Write a method with the following header to return a string format to represent the reverse order of the integer: public static String reverse(int number) For example, reverse(3456) returns 6543 and reverse(809340) returns 043908. Write a test program that prompts the user to enter an integer then displays its reversal. Convert integer to string and print reverse of integer as string. Do not use built-in toString. Use loop.
Write a method named changeStack that takes in a Stack of Integer objects named stackIn as...
Write a method named changeStack that takes in a Stack of Integer objects named stackIn as a parameter and returns another Stack of Integer objects. The returned Stack contains contents that is the result of switching the top half and the bottom half of the stackIn. But the ordering of integers in each half is NOT changed. In the case of odd-size stackIn, the middle element remains in the same position before and after the switch. This method is OUTSIDE...
Write a function (java static method) that computes the maximum of two integer. maxOfTwo(1, 2) →...
Write a function (java static method) that computes the maximum of two integer. maxOfTwo(1, 2) → 2 maxOfTwo(2, 1) → 2 maxOfTwo(1, 1) → 1 **(to start use): public int maxOfTwo(int a, int b) { And then complete the function (java static method)MaxOfThree so that it returns the maximum (highest) of the three int values passed as the parameters. maxOfThree(1, 2, 3) → 3 maxOfThree(0, 2, 1) → 2 maxOfThree(88, 4, 5) → 88 **(to start use): public int maxOfThree(int...
Consider the following Java program : public static void main (string args [ ]) { int...
Consider the following Java program : public static void main (string args [ ]) { int result, x ; x = 1 ; result = 0; while (x < = 10) { if (x%2 == 0) result + = x ; + + x ; } System.out.println(result) ; } } Which of the following will be the output of the above program? A. 35 B. 30 C. 45 D. 35 2. public static void main(String args[]) { int i =...
Given an integer queue called queue, write a method that will find the max element in...
Given an integer queue called queue, write a method that will find the max element in the queue. Use the STL to answer this question, you should keep queue elements in the same order and No other data structure can be used other than queue.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT