Java Reference
In-Depth Information
< Day Day Up >
Puzzle 89: Generic Drugs
Like the previous puzzle, this one makes heavy use of generics. Learning from our previous
mistakes, we refrain from using raw types. This program implements a simple linked list data
structure. The main program builds a list with two elements and dumps its contents. What does the
program print?
public class LinkedList<E> {
private Node<E> head = null;
private class Node<E> {
E value;
Node<E> next;
// Node constructor links the node as a new head
Node(E value) {
this.value = value;
this.next = head;
head = this;
}
}
public void add(E e) {
new Node<E>(e);
// Link node as new head
}
 
 
Search WWH ::




Custom Search