PhoneListNode newNode = new PhoneListNode("Bill C", "111-202-3331"); nodeptr.setNext(newNode);
Solution 16.4
The following condition is too general. It will cause the loop to exit as soon as a non-null node is encountered, whether or not the node matches the one being sought.
The PhoneList program will generate the following output, which has been edited slightly to improve its readability:
Phone Directory --------------- Roger M 997-0020 Roger W 997-0086 Rich P 997-0010 Jane M 997-2101 Stacy K 997-2517 Looking up numbers by name Roger M 997-0020 Rich P 997-0010 Stacy K 997-2517 Sorry. No entry for Mary P Removed Rich P 997-0010 Phone Directory --------------- Roger M 997-0020 Roger W 997-0086 Jane M 997-2101 Stacy K 997-2517 Removed Roger M 997-0020 Phone Directory ---------------
[Page 804]
Roger W 997-0086 Jane M 997-2101 Stacy K 997-2517 Removed Stacy K 997-2517 Phone Directory --------------- Roger W 997-0086 Jane M 997-2101 Removed Jane M 997-2101 Phone Directory --------------- Roger W 997-0086 Sorry. No entry for Jane M Phone Directory --------------- Roger W 997-0086 Removed Roger W 997-0086 Phone Directory --------------- Phone list is empty
Solution 16.6
Executing the following method calls will test whether it is possible to insert items into a list after items have been removed:
// Create and insert some nodes PhoneList list = new PhoneList(); list.insert(new PhoneListNode("Roger M", "997-0020")); list.insert(new PhoneListNode("Roger W", "997-0086")); System.out.println(list.remove("Roger M") ); list.insert(new PhoneListNode("Rich P", "997-0010")); System.out.println(list.remove("Roger W")); list.insert(new PhoneListNode("Jane M", "997-2101")); list.insert(new PhoneListNode("Stacy K", "997-2517")); System.out.println(list.remove("Jane M")); System.out.println(list.remove("Stacy K")); list.print(); // List should be empty
Solution 16.7
The List ADT program will produce the following output:
Generic List --------------- Hello World 8647 Roger M 997-0020 Jane M 997-2101 Stacy K 997-2517 Removed Stacy K 997-2517
[Page 805]
Generic List: Hello World 8647 Roger M 997-0020 Jane M 997-2101 Removed Jane M 997-2101 Generic List: Hello World 8647 Roger M 997-0020 Removed Hello World Generic List: 8647 Roger M 997-0020
Solution 16.8
Executing the following method calls will test whether it is possible to insert items into a List after items have been removed:
// Create and insert some nodes List list = new List(); list.insertAtFront(new PhoneRecord("Roger M", "997-0020")); list.insertAtFront(new PhoneRecord("Roger W", "997-0086")); System.out.println("Current List Elements"); list.print(); Object o = list.removeLast(); // Remove last element list.insertAtFront(o); // Insert at the front of the list System.out.println("Current List Elements"); list.print(); o = list.removeFirst(); System.out.println("Removed " + o.toString()); o = list.removeFirst(); System.out.println("Removed " + o.toString()); list.insertAtRear(o); System.out.println("Current List Elements"); list.print(); // List should have one element
Solution 16.9
The peek() method should just return the first node without deleting it:
public Object peek() { return head; }
[Page 806]
Solution 16.10
The peekLast() method can be modeled after the List.removeLast() method:
public Object peekLast() { if (isEmpty()) return null; else { Node current = head; // Start at head of listwhile (current.getNext() != null) // Find end of list current = current.getNext(); return current; // Return last node } } // peekLast()
Solution 16.11
The following class tests the java.util.Stack<E> class:
import java.util.*; public class StackTest{ public static void main( String argv[] ) { Stack<Character> stack = new Stack<Character>(); String string = "Hello this is a test string"; System.out.println("String: " + string); for (int k = 0; k < string.length(); k++) stack.push(new Character(string.charAt(k))); Character ch = null; String reversed = ""; while (!stack.empty()) { ch = stack.pop(); reversed = reversed + ch.charValue(); } System.out.println("Reversed String: " + reversed); } // main() } // StackTest class