Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.stream.Collectors;
- import java.util.stream.IntStream;
- public class ShiftStack {
- public static <T> Stack<T> shiftStack(Stack<T> stack, int shiftNumber) {
- java.util.Queue<T> queue = new ArrayDeque<>();
- int sizeStack = stack.size();
- /* Rest of the number from Stack to Queue */
- IntStream.range(0, sizeStack - shiftNumber).forEach(i -> queue.add(stack.pop()));
- System.out.println(String.format("S:%-50s Q:%-50s", stack, queue));
- /* All From Queue to Stack */
- int sizeQueue = queue.size();
- IntStream.range(0, sizeQueue).forEach(i -> stack.push(queue.poll()));
- System.out.println(String.format("S:%-50s Q:%-50s", stack, queue));
- /* All From Stack to Queue */
- IntStream.range(0, sizeStack).forEach(i -> queue.add(stack.pop()));
- System.out.println(String.format("S:%-50s Q:%-50s", stack, queue));
- /* All From Queue to Stack */
- sizeQueue = queue.size();
- IntStream.range(0, sizeQueue).forEach(i -> stack.push(queue.poll()));
- System.out.println(String.format("S:%-50s Q:%-50s", stack, queue));
- return stack;
- }
- public static void main(String args[]) {
- Integer array[] = IntStream.rangeClosed(1, 10).boxed().toArray(Integer[]::new);
- String toPrint = Arrays.stream(array)
- .map(i -> String.format("%d ", i))
- .collect(Collectors.joining("", "Original: ", "\n"));
- System.out.println(toPrint);
- int shiftNumber = 6;
- Stack<Integer> stack = new Stack<>();
- Arrays.stream(array).forEach(stack::push);
- toPrint = stack.stream().map(i -> String.format("%d ", i))
- .collect(Collectors.joining("", "Stack: ", "\n"));
- System.out.println(toPrint);
- System.out.println("\n Original Stack: " + stack + "\n");
- stack = shiftStack(stack, shiftNumber);
- System.out.println("\n Original Stack: " + stack + "\n");
- }
- }
- /**
- * Write a method shift that takes a stack of integers and an integer n as parameters and that shifts n values
- * from the bottom of the stack to the top of the stack. For example,
- * if a variable called s stores the following sequence of values:
- * <p>
- * bottom [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] top
- * If we make the call shift(s, 6); the method shifts the six values at the bottom of the stack
- * to the top of the stack and leaves the other values in the same order producing:
- * <p>
- * bottom [7, 8, 9, 10, 6, 5, 4, 3, 2, 1] top
- * Notice that the value that was at the bottom of the stack is now at the top,
- * the value that was second from the bottom is now second from the top,
- * the value that was third from the bottom is now third from the top, and so on,
- * and that the four values not involved in the shift are now at the bottom of the stack in their original order.
- * If s had stored these values instead:
- * <p>
- * bottom [7, 23, -7, 0, 22, -8, 4, 5] top
- * If we make the following call: shift(s, 3); then s should store these values after the call:
- * <p>
- * bottom [0, 22, -8, 4, 5, -7, 23, 7] top
- * You are to use one queue as auxiliary storage to solve this problem. You may assume that the parameter n is >= 0
- * and not larger than the number of elements in the stack.
- */
Advertisement
Add Comment
Please, Sign In to add comment