import java.io.*; import com.mw.io.*; import structure.*; public class MySinglyLinkedList extends SinglyLinkedList { public void reverse () { SinglyLinkedListElement p = null; SinglyLinkedListElement q = head; while (q != null) { head = q.next(); q.setNext(p); p = q; q = head; } head = p; } } class ListBuilder2 { // Program to build a singly-linked list, from a series of // words typed by the user, and then display it. // The class SinglyLinkedList is used to supply the methods // for building the list. public static void main (String args[]) { SystemInput sysIn = new SystemInput(); ReadStream r = new ReadStream(); String s; MySinglyLinkedList words = new MySinglyLinkedList(); // List of words to be built System.out.println("Enter a series of words, followed by Command-D"); s = r.readString(); while (! r.eof()) { // link s to the tail end of the list words.addToTail(s); s = r.readString(); } // now reverse the list words.reverse(); // now display the list int count = words.size(); // size of the list System.out.println("The words entered are, in reverse order:"); while (!words.isEmpty()) { s = (String)words.removeFromHead(); System.out.println(s); } System.out.println(count + " words were entered."); } }