In: Computer Science
/**
* Write a recursive function that accepts a Queue<Integer>. It
* should change every int in this queue to be double its original
* value. You may NOT use loops or any other data structures besides
* the queue passed in as a parameter. You may use a helper function.
* @param q
*/
public static void doubleElements(Queue<Integer> q) {}
import java.util.ArrayDeque; import java.util.Queue; public class DoubleElements { /** * Write a recursive function that accepts a Queue<Integer>. It * <p> * should change every int in this queue to be double its original * <p> * value. You may NOT use loops or any other data structures besides * <p> * the queue passed in as a parameter. You may use a helper function. * * @param q */ public static void doubleElementsHelper(Queue<Integer> q) { doubleElementsHelper(q, q.size()); } public static void doubleElementsHelper(Queue<Integer> q, int size) { if (size > 0) { int num = q.remove(); q.add(num*2); doubleElementsHelper(q, size-1); } } public static void main(String[] args) { Queue<Integer> queue = new ArrayDeque<>(); queue.add(1); queue.add(2); queue.add(3); System.out.println("Original queue: " + queue); doubleElementsHelper(queue); System.out.println("Modified queue: " + queue); } }