import java.io.*; import com.mw.io.*; import structure.*; public class ListBuilder { // Program to build a singly-linked list, from a series of // words typed by the user, and then display it. // The class SinglyLinkedListElement is used to supply the // operations for linking list elements together. public static void main (String args[]) { SystemInput sysIn = new SystemInput(); ReadStream r = new ReadStream(); String s; SinglyLinkedListElement head = null; // pointer to the first element int count = 0; // size of the list SinglyLinkedListElement member = null; // next member to be inserted SinglyLinkedListElement previous = null; // previous member inserted 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 member = new SinglyLinkedListElement(s); if (head == null) head = new SinglyLinkedListElement(null, member); else previous.setNext(member); count++; // and prepare for linking the next line of input previous = member; s = r.readString(); } // now display the list System.out.println("The words entered are:"); member = head.next(); while (member!=null) { System.out.println(member.value()); member = member.next(); } System.out.println(count + " words were entered."); } }