16.2.1.1. New String Node.
Solution.
Either of these statements would work:
Node node = new Node(new String("Hello"));
Node node = new Node("Hello");
Abstract Data Type (ADT) | linked list |
binary search tree | list |
data structure | pop |
dequeue | push |
dynamic structure | queue |
enqueue | reference |
first-in–first-out (FIFO) | self-referential object |
generic type | stack |
Java collections framework | static structure |
key | traverse |
last-in–first-out (LIFO) | value |
link |
ArrayList
is an example of a dynamic structure, one whose size can grow and shrink during a program’s execution.Node
class is an example of a self-referential class. It contains a link variable that refers to a Node
. By assigning references to the link variable, Node
s can be chained together into a linked list. In addition to their link variables, Node
s contain data variables, which should be accessible through public methods.Object
s or int
s) contained in the nodes that make up the list, and the operations are insertion, removal, and tests of whether the list is empty.public
and private
aspects, is perfectly suited to implement an ADT.Stack
ADT can easily be defined as a subclass of List
. Stacks are used for managing the method call and return in most programming languages.Queue
ADT can easily be defined as a subclass of List
. Queues are used for managing the various lists used by the CPU scheduler—such as the ready, waiting, and blocked queues.Node node = new Node(new String("Hello"));
Node node = new Node("Hello");
Object
.Node node = new Node(new Integer(100));
PhoneListNode newNode = new PhoneListNode("Bill C", "111-202-3331");
nodeptr.setNext(newNode);
// 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"));
list.print();
System.out.println(list.remove("Roger M") );
System.out.println(list.remove("Roger W") );
list.print();
// List should be empty
list.insert(new PhoneListNode("Rich P", "997-0010"));
list.insert(new PhoneListNode("Jane M", "997-2101"));
list.insert(new PhoneListNode("Stacy K", "997-2517"));
list.print();
System.out.println(list.remove("Jane M"));
System.out.println(list.remove("Stacy K"));
System.out.println(list.remove("Rich P"));
list.print();
// List should be empty
List
after all 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());
System.out.println("Current List Elements");
list.print(); // List should be empty.
list.insertAtRear(o);
System.out.println("Current List Elements");
list.print(); // List should have one element
Stack
Class
public static void main(String argv[]) {
Stack stack = new Stack();
String string = "Hello this is a test string";
System.out.println("String: " + string);
for (int k = 0; k < string.length(); k++)
stack.push(Character.valueOf(string.charAt(k)));
Object o = null;
String reversed = "";
while (!stack.isEmpty()) {
o = stack.pop();
reversed = reversed + o.toString();
}
System.out.println("Reversed String: " + reversed);
} // main()
peek()
method should just return the first node without deleting it: public Object peek() {
return head;
}
Queue
Class
public static void main(String argv[]) {
Queue queue = new Queue();
String string = "Hello this is a test string";
System.out.println("String: " + string);
for (int k = 0; k < string.length(); k++)
queue.enqueue(Character.valueOf(string.charAt(k)));
System.out.println("The current queue:");
queue.print();
Object o = null;
System.out.println("Dequeuing:");
while (!queue.isEmpty()) {
o = queue.dequeue();
System.out.print(o.toString());
}
} // main()
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 list
while (current.getNext() != null) // Find end of list
current = current.getNext();
return current; // Return last node
}
} // peekLast()
java.util.Stack<E>
class
java.util.Stack<E>
class: import java.util.*;
public class StackTest{
public static void main( String args[] ) {
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(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
Set
and Map
InterfacesMap<K,V>
Interface
import java.util.*;
public class TranslateMap {
public static void testMap() {
Map<String, String> theMap =
new TreeMap<String,String>();
// new HashMap<K,V>(); could also be used
theMap.put("cat", "gato");
theMap.put("dog", "perro");
System.out.print("cat in Spanish: ");
System.out.println(theMap.get("cat"));
System.out.print("dog in Spanish: ");
System.out.println(theMap.get("dog"));
} // testMap
public static void main(String args[]) {
testMap();
} // main()
}
PhoneTree
:public void insert(String nam, String pho){
if (root == null)
root = new PhoneTreeNode(nam, pho);
else
root.insert(nam, pho);
}
PhoneTreeNode
: public void insert(String nam, String pho){
if (name.compareTo(nam) <= 0) { // name <= nam
if (right == null)
right = new PhoneTreeNode(nam, pho);
else
right.insert(nam, pho);
} else { // name > nam
if (left == null)
left = new PhoneTreeNode(nam, pho);
else
left.insert(nam, pho);
} // else
} // insert