Chapter 9: "Stacks and Queues" Data Structures with Java, Rick Mercer Answers to Self-Check Questions 9-1 z y x 9-2 true <<< 9-3 false 1 2 3 true 9-4 Check symbols in Test2.java Abc.java:2 expecting ] Abc.java:4 expecting } Abc.java:4 expecting } missing } 4 errors 9-5 Java 9-6 first first first ... first until someone terminates the program or the power goes out 9-7 // OurQueue does not implement Iterable so the enhanced for loop can not be used int size = intQueue.size(); for (int j = 1; j <= size; j++) { int nextInt = intQueue.peek(); if (nextInt % 2 != 0) System.out.println(nextInt + " is odd"); else System.out.println(nextInt + " is even"); intQueue.remove(); intQueue.add(nextInt); } 9-8 front o-> |5.6|o->|7.8|<-o back 9-9 front o-> |"a"|o->|"7.8"|<-o back 9-10 @Override public String toString() { String result = "["; // Concatenate all but the last one (if size > 0) Node ref = front; while (ref != back) { result += ref.data + ", "; ref = ref.next; } // Last element does not have ", " after it if (ref != null) result += ref.data; result += "]"; return result; } 9-11 public E remove() throws NoSuchElementException { if (this.isEmpty()) throw new NoSuchElementException(); E frontElement = front.data; front = front.next; if (front == null) front = back = null; return frontElement; }