Filip_Markoski

[ADSx] - Interleave Queue

Jan 7th, 2018
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.81 KB | None | 0 0
  1. import java.util.ArrayDeque;
  2. import java.util.Arrays;
  3. import java.util.Queue;
  4. import java.util.Stack;
  5. import java.util.stream.IntStream;
  6.  
  7. public class InterleaveQueue {
  8.  
  9.     public static final int I_DONT_KNOW_WHY = 3;
  10.  
  11.     public static <T extends Number> Queue<T> interleave(Queue<T> queue) {
  12.         Stack<T> stack = new Stack<>();
  13.         int sizeQueue = queue.size();
  14.         int sizeStack;
  15.  
  16.         for (int j = 0; j < I_DONT_KNOW_WHY; j++) {
  17.             /* First half goes in Stack */
  18.             IntStream.range(0, sizeQueue / 2).forEach(i -> stack.push(queue.poll()));
  19.             System.out.printf("Q:%-40s     S:%-40s\n", queue, stack);
  20.  
  21.             /* All from Stack into Queue */
  22.             sizeStack = stack.size();
  23.             IntStream.range(0, sizeStack).forEach(i -> queue.add(stack.pop()));
  24.             System.out.printf("Q:%-40s     S:%-40s\n", queue, stack);
  25.         }
  26.  
  27.         /* First half goes in Stack */
  28.         IntStream.range(0, sizeQueue / 2).forEach(i -> stack.push(queue.poll()));
  29.         System.out.printf("Q:%-40s     S:%-40s\n", queue, stack);
  30.  
  31.         for (int i = 0; i < sizeQueue / 2; i++) {
  32.             T poll = queue.poll();
  33.             T pop = stack.pop();
  34.             queue.add(poll);
  35.             queue.add(pop);
  36.         }
  37.  
  38.         System.out.printf("Q:%-40s     S:%-40s\n", queue, stack);
  39.  
  40.         return queue;
  41.     }
  42.  
  43.     public static void main(String args[]) {
  44.         Integer array[] = IntStream.rangeClosed(1, 10).boxed().toArray(Integer[]::new);
  45.         Queue<Integer> queue = new ArrayDeque<>();
  46.  
  47.         if (queue.size() % 2 != 0) throw new IllegalArgumentException();
  48.  
  49.         Arrays.stream(array).forEach(queue::add);
  50.  
  51.         System.out.printf("\n Original Queue: %s\n", queue);
  52.  
  53.         queue = interleave(queue);
  54.         System.out.printf("\n Original Queue: %s\n", queue);
  55.     }
  56. }
  57.  
  58.  
  59. /**
  60.  * Write a method interleave that accepts a queue of integers as a parameter and rearranges
  61.  * the elements by alternating the elements from the first half of the queue with those from the second half of the queue.
  62.  * For example, suppose a variable q stores the following sequence of values:
  63.  * <p>
  64.  * front [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] back
  65.  * If we make the call of interleave(q);, the queue should store the following values after the call:
  66.  * <p>
  67.  * front [1, 6, 2, 7, 3, 8, 4, 9, 5, 10] back
  68.  * <p>
  69.  * Your method should throw an IllegalArgumentException if the queue does not have even size.
  70.  * You may use one stack as auxiliary storage to solve this problem. You may not use any other auxiliary data
  71.  * structures to solve this problem, although you can have as many simple variables as you like.
  72.  * You may not use recursion to solve this problem. For full credit, your solution must run in O(n) time,
  73.  * where n represents the size of the queue.
  74.  */
Advertisement
Add Comment
Please, Sign In to add comment