Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayDeque;
- import java.util.Arrays;
- import java.util.Queue;
- import java.util.Stack;
- import java.util.stream.IntStream;
- public class IsConsecutiveStack {
- static <T extends Number> boolean isConsecutive(Stack<T> stack) {
- Queue<T> queue = new ArrayDeque<>();
- boolean isConsecutive = true;
- int sizeStack;
- int sizeQueue;
- /* All from Stack to Queue */
- sizeStack = stack.size();
- IntStream.range(0, sizeStack).forEach(i -> queue.add(stack.pop()));
- System.out.printf("S:%-40s Q:%-40s\n", stack, queue);
- sizeQueue = queue.size();
- /* All from Queue to Stack */
- IntStream.range(0, sizeQueue).forEach(i -> stack.push(queue.poll()));
- System.out.printf("S:%-40s Q:%-40s\n", stack, queue);
- T previous = stack.pop();
- queue.add(previous);
- for (int i = 1; i < sizeStack; i++) {
- T current = stack.pop();
- queue.add(current);
- //System.out.printf("P:%d C:%d\n", previous.intValue(), current.intValue());
- if (previous.intValue() != current.intValue() - 1) {
- isConsecutive = false;
- break;
- }
- previous = current;
- }
- System.out.printf("S:%-40s Q:%-40s\n", stack, queue);
- /* All from Queue to Stack */
- sizeQueue = queue.size();
- IntStream.range(0, sizeQueue).forEach(i -> stack.push(queue.poll()));
- System.out.printf("S:%-40s Q:%-40s\n", stack, queue);
- return isConsecutive;
- }
- public static void main(String args[]) {
- Integer array[] = IntStream.rangeClosed(1, 10).boxed().toArray(Integer[]::new);
- Stack<Integer> stack = new Stack<>();
- Arrays.stream(array).forEach(stack::push);
- stack.push(stack.pop() + 10);
- System.out.printf("\n Original Stack: %s\n", stack);
- System.out.println(isConsecutive(stack));
- System.out.printf("\n Original Stack: %s\n", stack);
- }
- }
- /**
- * Write a method isConsecutive that takes a stack of integers as a parameter and that returns whether or not the stack
- * contains a sequence of consecutive integers starting from the bottom of the stack (returning true if it does,
- * returning false if it does not). Consecutive integers are integers that come one after the other, as in 5, 6, 7, 8, 9, etc.
- * So if a stack s stores the following values:
- * <p>
- * bottom [3, 4, 5, 6, 7, 8, 9, 10] top
- * Then the call of isConsecutive(s) should return true. If the stack had instead contained this set of values:
- * <p>
- * bottom [3, 4, 5, 6, 7, 8, 9, 10, 12] top
- * Then the call should return false because the numbers 10 and 12 are not consecutive.
- * Notice that we look at the numbers starting at the bottom of the stack.
- * The following sequence of values would be consecutive except for the fact that it appears in reverse order,
- * so the method would return false:
- * <p>
- * bottom [3, 2, 1] top
- * Your method must restore the stack so that it stores the same sequence of values after the call as it did before.
- * Any stack with fewer than two values should be considered to be a list of consecutive integers.
- * You may use one queue as auxiliary storage to solve this problem.
- */
Advertisement
Add Comment
Please, Sign In to add comment