Question

In: Computer Science

/** * Write a recursive function that accepts a stack of integers and * replaces each...

/**

* Write a recursive function that accepts a stack of integers and

* replaces each int with two copies of that integer. For example,

* calling repeatStack and passing in a stack of { 1, 2, 3} would change

* the stack to hold { 1, 1, 2, 2, 3, 3}. Do not use any loops. Do not use

* any data structures other than the stack passed in as a parameter.

* @param stack

*/

public static void repeatStack(Stack<Integer> stack) {}

Solutions

Expert Solution

import java.util.Stack;

public class RepeatStack {

    /**
     * Write a recursive function that accepts a stack of integers and
     * <p>
     * replaces each int with two copies of that integer. For example,
     * <p>
     * calling repeatStack and passing in a stack of { 1, 2, 3} would change
     * <p>
     * the stack to hold { 1, 1, 2, 2, 3, 3}. Do not use any loops. Do not use
     * <p>
     * any data structures other than the stack passed in as a parameter.
     *
     * @param stack
     */
    public static void repeatStack(Stack<Integer> stack) {
        if (!stack.isEmpty()) {
            int num = stack.pop();
            repeatStack(stack);
            stack.push(num);
            stack.push(num);
        }
    }

    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        System.out.println("Original stack: " + stack);
        repeatStack(stack);
        System.out.println("Modified stack: " + stack);
    }
}

Related Solutions

In Java, write a recursive function that accepts a string as its argument and prints the...
In Java, write a recursive function that accepts a string as its argument and prints the string in reverse order. Demonstrate the function in a driver program.
/** * Write a recursive function that accepts a Queue<Integer>. It * should change every int...
/** * 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) {}
1) Write a function searchValue that accepts an array of integers, the size of the array,...
1) Write a function searchValue that accepts an array of integers, the size of the array, and an integer. Find the last occurrence of the integer passed in as an input argument in the array. Return the index of the last occurrence of the value. If the value is not found, return a -1 2) Write the line of code to call the previous function assuming you have an array vec with length n, and are looking for the number...
In this third part, write a function that accepts an array of integers and its size as arguments.
in C++In this third part, write a function that accepts an array of integers and its size as arguments. The function should create a new array that is of half size the argument array (round up the fraction if the size of the argument array is odd). The value of the first element (Element 0) of the new array should be the sum of the first two elements of the argument array (Element 0 and 1). Element 1 of the...
write a function mean_value that accepts up to four integers and returns their possibly floating point...
write a function mean_value that accepts up to four integers and returns their possibly floating point mean_value. the function should be able to work with any number of inputs from one to four. ( it just returns the input if there is only one)
Write a recursive function named multiply that takes two positive integers as parameters and returns the...
Write a recursive function named multiply that takes two positive integers as parameters and returns the product of those two numbers (the result from multiplying them together). Your program should not use multiplication - it should find the result by using only addition. To get your thinking on the right track: 7 * 4 = 7 + (7 * 3) 7 * 3 = 7 + (7 * 2) 7 * 2 = 7 + (7 * 1) 7 *...
Write a function file that accepts each of the four vectors as inputs. The function file...
Write a function file that accepts each of the four vectors as inputs. The function file should plot the data on different graphs in the same window. You need to add axis labels and graph titles. time- 0 5 10 15 20 25 30 A1- 0 7 11 19 15 14 7 A2- 0 10 15 21 16 11 13 A3- 0 9 13 17 22 25 21
Without using method size(), write recursive method stackSize(Stack > s1 ) that receives a stack and...
Without using method size(), write recursive method stackSize(Stack > s1 ) that receives a stack and returns number of elements in the stack. The elements in the stack should not be changed after calling this method. please do it in java
Write a recursive function, max_in_list(my_list), which takes an non-empty list, my_list, of integers as a parameter....
Write a recursive function, max_in_list(my_list), which takes an non-empty list, my_list, of integers as a parameter. This function calculates and returns the largest value in the list. The base case will probably deal with the scenario where the list has just one value. The recursive case will probably call the function recursively using the original list, but with one item removed. Note: This function has to be recursive; you are not allowed to use loops to solve this problem! Test...
7. Write a function that accepts a sentence as the argument and converts each word to...
7. Write a function that accepts a sentence as the argument and converts each word to “Pig Latin.” In one version, to convert a word to Pig Latin, you remove the first letter and place that letter at the end of the word. Then you append the string “ay” to the word. Here is an example: English: I SLEPT MOST OF THE NIGHTPIG LATIN: IAY LEPTSAY OSTMAY FOAY HETAY IGHTNAY.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT