// Linked list example from Lewis and Loftus, page 500 class Book { private String title; public Book (String newTitle) { title = newTitle; } public String toString () { return title; } } class BookList { private BookNode list; public BookList() { list = null; } // Creates and adds a new Book object to the end of list public void add (Book newBook) { BookNode node = new BookNode (newBook); BookNode current; if (list == null) list = node; else { current = list; while (current.next != null) current = current.next; current.next = node; } } public String toString () { String result = ""; BookNode current = list; while (current != null) { result += current.book.toString() + "\n"; current = current.next; } return result; } // An inner class that represents a node in list private class BookNode { public Book book; public BookNode next; public BookNode (Book theBook) { book = theBook; next = null; } } } public class Library { public static void main (String[] args) { BookList books = new BookList(); books.add (new Book("The Hitchhiker's Guide to the Galaxy")); books.add (new Book("Jonathan Livingston Seagull")); books.add (new Book("A Tale of Two Cities")); books.add (new Book("Java Software Solutions")); System.out.println (books); } }