Filip_Markoski

[ADSx] - Shift Stack

Jan 7th, 2018
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.33 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3. import java.util.stream.IntStream;
  4.  
  5. public class ShiftStack {
  6.  
  7.     public static <T> Stack<T> shiftStack(Stack<T> stack, int shiftNumber) {
  8.         java.util.Queue<T> queue = new ArrayDeque<>();
  9.         int sizeStack = stack.size();
  10.  
  11.         /* Rest of the number from Stack to Queue */
  12.         IntStream.range(0, sizeStack - shiftNumber).forEach(i -> queue.add(stack.pop()));
  13.         System.out.println(String.format("S:%-50s     Q:%-50s", stack, queue));
  14.  
  15.         /* All From Queue to Stack */
  16.         int sizeQueue = queue.size();
  17.         IntStream.range(0, sizeQueue).forEach(i -> stack.push(queue.poll()));
  18.         System.out.println(String.format("S:%-50s     Q:%-50s", stack, queue));
  19.  
  20.         /* All From Stack to Queue */
  21.         IntStream.range(0, sizeStack).forEach(i -> queue.add(stack.pop()));
  22.         System.out.println(String.format("S:%-50s     Q:%-50s", stack, queue));
  23.  
  24.         /* All From Queue to Stack */
  25.         sizeQueue = queue.size();
  26.         IntStream.range(0, sizeQueue).forEach(i -> stack.push(queue.poll()));
  27.         System.out.println(String.format("S:%-50s     Q:%-50s", stack, queue));
  28.         return stack;
  29.     }
  30.  
  31.     public static void main(String args[]) {
  32.  
  33.         Integer array[] = IntStream.rangeClosed(1, 10).boxed().toArray(Integer[]::new);
  34.         String toPrint = Arrays.stream(array)
  35.                 .map(i -> String.format("%d ", i))
  36.                 .collect(Collectors.joining("", "Original: ", "\n"));
  37.         System.out.println(toPrint);
  38.  
  39.         int shiftNumber = 6;
  40.  
  41.         Stack<Integer> stack = new Stack<>();
  42.         Arrays.stream(array).forEach(stack::push);
  43.  
  44.         toPrint = stack.stream().map(i -> String.format("%d ", i))
  45.                 .collect(Collectors.joining("", "Stack: ", "\n"));
  46.         System.out.println(toPrint);
  47.  
  48.         System.out.println("\n Original Stack: " + stack + "\n");
  49.         stack = shiftStack(stack, shiftNumber);
  50.         System.out.println("\n Original Stack: " + stack + "\n");
  51.     }
  52. }
  53.  
  54. /**
  55.  * Write a method shift that takes a stack of integers and an integer n as parameters and that shifts n values
  56.  * from the bottom of the stack to the top of the stack. For example,
  57.  * if a variable called s stores the following sequence of values:
  58.  * <p>
  59.  * bottom [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] top
  60.  * If we make the call shift(s, 6); the method shifts the six values at the bottom of the stack
  61.  * to the top of the stack and leaves the other values in the same order producing:
  62.  * <p>
  63.  * bottom [7, 8, 9, 10, 6, 5, 4, 3, 2, 1] top
  64.  * Notice that the value that was at the bottom of the stack is now at the top,
  65.  * the value that was second from the bottom is now second from the top,
  66.  * the value that was third from the bottom is now third from the top, and so on,
  67.  * and that the four values not involved in the shift are now at the bottom of the stack in their original order.
  68.  * If s had stored these values instead:
  69.  * <p>
  70.  * bottom [7, 23, -7, 0, 22, -8, 4, 5] top
  71.  * If we make the following call: shift(s, 3); then s should store these values after the call:
  72.  * <p>
  73.  * bottom [0, 22, -8, 4, 5, -7, 23, 7] top
  74.  * You are to use one queue as auxiliary storage to solve this problem. You may assume that the parameter n is >= 0
  75.  * and not larger than the number of elements in the stack.
  76.  */
Advertisement
Add Comment
Please, Sign In to add comment