public class QueueTest{ public static void main(String[] args){ Queue<Integer> orders = new LinkedList<Integer>(); orders.add("Orange Juice"); ordres.add("Mangoes"); orders.add("Almond Milk"); orders.add("Eggs"); orders.add("Blueberries"); String a = orders.poll(); String b = orders.peek(); String c = orders.poll(); String d = orders.peek(); System.out.println(a + ", " + b + ", " + c + ", " + d); } }
public class QueueTest{ public static void main(String[] args){ Queue<Integer> queue = new LinkedList<Integer>(); Stack<Integer> stack = new Stack<Integer>(); for (int i = 1; i <= 9; i += 2) queue.add(i); while (!queue.isEmpty()) { stack.add(queue.poll()); } while (!stack.isEmpty()) { queue.add(stack.pop()); } } }
Example answer format (spacing between the numbers): 1, 2, 3, 4, 5
public class Test{ public static void main(String[] args){ PriorityQueue<Integer> queue = new PriorityQueue<Integer>(); /* a is first iteration, b is second iteration, ... , e is fifth iteration */ for (int i = 5; i >= 0; i--) { queue.add(i); System.out.println(queue); // 1. a, b, c, d, e, f } String result = ""; while (!queue.isEmpty()) { result += queue.poll() + " "; } System.out.println(result.trim()); // 2. a. } }
a = 5
b = 4, 5
public class QueueTest{ public static void main(String[] args){ Queue<Integer> q1 = new LinkedList<Integer>(); Queue<Integer> q2 = new LinkedList<Integer>(); int limit = 5; for(int i = 0; i < limit; i++) { q1.add((int) Math.pow(i, i)); q2.add((limit - i)*(limit - i)); } System.out.println(q1); // 1. System.out.println(q2); // 2. } }
public class Test{ public static void main(String[] args){ Queue<Character> q = new LinkedList<Character>(); String s = "I love CMSC132 more than pizza!"; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) >= 'a' && s.charAt(i) <= 'z') { q.add(s.charAt(i)); } } System.out.println(q.size()); //1 } }