Challenge #3, Shuffle the Queue.

Iterate through the Queue and change data with another random position in the queue.## 2017 FRQ Q1 (Methods and Control Structures question type)

import java.lang.Math;

Queue<Object> queue = new LinkedList<>();
queue.add(1);
queue.add(2);
queue.add(3);
queue.add(4);
queue.add(5);
queue.add(6);
queue.add(7);
queue.add(8);
queue.add(9);

System.out.println("Ordered Queue");
System.out.println(queue);


ArrayList arr = new ArrayList(queue);
Queue<Object> queue2 = new LinkedList<>();
for (int i = arr.size(); i > 0; i--) {
    int rand = (int) (Math.random() * i) + 1;
    queue2.add(arr.get(rand-1));
    arr.remove(rand-1);
}

System.out.println("Shuffled Queue");
System.out.println(queue2);
Ordered Queue
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Shuffled Queue
[4, 6, 9, 5, 1, 2, 7, 3, 8]

Challenge #4, Build a Stack and use it to reverse the order of a Queue.

The Queue is a LIFO Data Structure, the Stack is a FIFO data structure, so code is similar but most everything is reversed.

import java.lang.Math;
import java.util.*;

Queue<Object> queue = new LinkedList<>();
queue.add(1);
queue.add(2);
queue.add(3);
queue.add(4);
queue.add(5);
queue.add(6);
queue.add(7);
queue.add(8);
queue.add(9);

ArrayList arr = new ArrayList(queue);

System.out.println("Queue");
while (queue.size() > 0) {
    System.out.print(queue.remove());
    System.out.print(' ');
}
System.out.println();

Stack<Object> stack = new Stack<>();
for (Object num: arr) {
    stack.push(num);
}

System.out.println("Stack");
while (stack.size() > 0) {
    System.out.print(stack.pop());
    System.out.print(' ');
}
Queue
1 2 3 4 5 6 7 8 9 
Stack
9 8 7 6 5 4 3 2 1