Guest User

Untitled

a guest
Nov 19th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. THE PROBLEM
  2. Write a method stutter that accepts a Stack of integers as a parameter and replaces every value in the stack with two occurrences of that value.
  3. For example, suppose a stack stores these values:
  4.  
  5. bottom [3, 7, 1, 14, 9] top
  6. Then the stack should store the following values after the method terminates:
  7.  
  8. bottom [3, 3, 7, 7, 1, 1, 14, 14, 9, 9] top
  9. Notice that you must preserve the original order. In the original stack the 9 was at the top and would have been popped first.
  10. In the new stack the two 9s would be the first values popped from the stack.
  11. Also, you must not use any auxillary data structures to solve this problem.
  12. If the original stack is empty, the result should be empty as well.
  13.  
  14. MY SOLUTION (doesn't produce expected output this is where I need help)
  15. public void stutter(Stack<Integer> s) 
  16. {
  17.     if (s.isEmpty())
  18.     {
  19.         System.out.print("");
  20.     }
  21.     else
  22.     {
  23.         int temp = s.pop();
  24.         stutter(s);
  25.         s.push(temp);
  26.         s.push(temp);
  27.       
  28.     }
  29.     System.out.print(s);
  30. }
  31.  
  32. //expected output is [1, 1, 2, 2]
  33. //my output is [][1, 1][1, 1, 2, 2]
Add Comment
Please, Sign In to add comment