Filip_Markoski

[ADSx] - IsConsecutive Stack

Jan 7th, 2018
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.29 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 IsConsecutiveStack {
  8.  
  9.     static <T extends Number> boolean isConsecutive(Stack<T> stack) {
  10.         Queue<T> queue = new ArrayDeque<>();
  11.         boolean isConsecutive = true;
  12.  
  13.         int sizeStack;
  14.         int sizeQueue;
  15.  
  16.         /* All from Stack to Queue */
  17.         sizeStack = stack.size();
  18.         IntStream.range(0, sizeStack).forEach(i -> queue.add(stack.pop()));
  19.         System.out.printf("S:%-40s     Q:%-40s\n", stack, queue);
  20.  
  21.         sizeQueue = queue.size();
  22.  
  23.         /* All from Queue to Stack */
  24.         IntStream.range(0, sizeQueue).forEach(i -> stack.push(queue.poll()));
  25.         System.out.printf("S:%-40s     Q:%-40s\n", stack, queue);
  26.  
  27.         T previous = stack.pop();
  28.         queue.add(previous);
  29.         for (int i = 1; i < sizeStack; i++) {
  30.             T current = stack.pop();
  31.             queue.add(current);
  32.             //System.out.printf("P:%d   C:%d\n", previous.intValue(), current.intValue());
  33.             if (previous.intValue() != current.intValue() - 1) {
  34.                 isConsecutive = false;
  35.                 break;
  36.             }
  37.             previous = current;
  38.         }
  39.  
  40.         System.out.printf("S:%-40s     Q:%-40s\n", stack, queue);
  41.  
  42.         /* All from Queue to Stack */
  43.         sizeQueue = queue.size();
  44.         IntStream.range(0, sizeQueue).forEach(i -> stack.push(queue.poll()));
  45.         System.out.printf("S:%-40s     Q:%-40s\n", stack, queue);
  46.  
  47.         return isConsecutive;
  48.     }
  49.  
  50.     public static void main(String args[]) {
  51.         Integer array[] = IntStream.rangeClosed(1, 10).boxed().toArray(Integer[]::new);
  52.         Stack<Integer> stack = new Stack<>();
  53.         Arrays.stream(array).forEach(stack::push);
  54.  
  55.         stack.push(stack.pop() + 10);
  56.  
  57.         System.out.printf("\n Original Stack: %s\n", stack);
  58.  
  59.         System.out.println(isConsecutive(stack));
  60.         System.out.printf("\n Original Stack: %s\n", stack);
  61.     }
  62. }
  63.  
  64. /**
  65.  * Write a method isConsecutive that takes a stack of integers as a parameter and that returns whether or not the stack
  66.  * contains a sequence of consecutive integers starting from the bottom of the stack (returning true if it does,
  67.  * returning false if it does not). Consecutive integers are integers that come one after the other, as in 5, 6, 7, 8, 9, etc.
  68.  * So if a stack s stores the following values:
  69.  * <p>
  70.  * bottom [3, 4, 5, 6, 7, 8, 9, 10] top
  71.  * Then the call of isConsecutive(s) should return true. If the stack had instead contained this set of values:
  72.  * <p>
  73.  * bottom [3, 4, 5, 6, 7, 8, 9, 10, 12] top
  74.  * Then the call should return false because the numbers 10 and 12 are not consecutive.
  75.  * Notice that we look at the numbers starting at the bottom of the stack.
  76.  * The following sequence of values would be consecutive except for the fact that it appears in reverse order,
  77.  * so the method would return false:
  78.  * <p>
  79.  * bottom [3, 2, 1] top
  80.  * Your method must restore the stack so that it stores the same sequence of values after the call as it did before.
  81.  * Any stack with fewer than two values should be considered to be a list of consecutive integers.
  82.  * You may use one queue as auxiliary storage to solve this problem.
  83.  */
Advertisement
Add Comment
Please, Sign In to add comment