/* file: node.java * Basic Algorithms, Fall 1999, Yap * * PROGRAMMING SOLUTION * for Question 4, Homework 2 * by Archisman Rudra (T.A.) * */ import java.io.*; // to use DataInputStream interface nodeInterface { //================================================== // basic methods //================================================== public void setItem(Object i); public void setItem(node n); // use only n.item public void setNext(node n); public node getNext(); public Object getItem(); public void printItem(); public void print(); // Prints each item of list // in its own line //================================================== // methods for File I/O //================================================== public int readString(DataInputStream dis); // WE WILL PROVIDE THIS FUNCTION (but study it!) // Reads a new string into this->item // Returns the number of bytes read (-1 is EOF) public int readFromFile(String fn); // WE WILL PROVIDE THIS FUNCTION (but study it!) // Creates a list of nodes whose items are the // input lines of the file "fn" // Returns the number of nodes in list // If the returned value is <0, error in opening files } //nodeInterface // THE SKELETON OF AN IMPLEMENTATION... public class node implements nodeInterface { //================================================== // variables //================================================== private Object item; private node next; //================================================== // constructors //================================================== node() { this(null,null); } node(Object e, node n) { item=e; next=n; } //================================================== // basic methods //================================================== // DO YOUR IMPLEMENTATION HERE public void setItem(Object i) { item = i;} public void setItem(node n) // use only n.item { item = n.getItem();} public void setNext(node n) { next = n;} public node getNext() {return next;} public Object getItem() {return item;} public void printItem() {System.out.print(item);} public void print() // Prints each item of list // in its own line { node pointer = this; for(; pointer != null; pointer = pointer.getNext()) { pointer.printItem(); System.out.print("\n"); } } //================================================== // methods for File I/O //================================================== public int readString(DataInputStream dis) // reads a new string into this->item // returns the number of bytes read (-1 is EOF) // COMMENT: // (1) This method is basically the same as the // deprecated java.io method called readLine. // We prefer to avoid deprecated methods. // (2) The code below assumes that the input file is // an ASCII file in which each line is terminated // by the sequence "\r\n". Please write your // own variant (called "myReadString") if your // operating system has a different convention. // For instance, on Linux, the terminating sequence // is "\n". { byte[] cc = new byte[100]; byte c; boolean EOF_FLAG = false; int i = 0; try{ while( !EOF_FLAG ) { c = dis.readByte(); if (c == '\n') { EOF_FLAG = true; // dis.readByte(); // following '\r' is '\n', } // which we just discard else { cc[i] = c; i++; } } } catch(Exception e) { item = null; return -1; }; item = new String(cc, 0, i); return i; } //readString //================================================== // readFromFile( fn ) : constructs a list from a file "fn" //================================================== public int readFromFile(String fn) // returns the number of nodes in list // the first node in the read-in list is "this" // if the returned value is <0, error in opening files { FileInputStream is = null; DataInputStream dis = null; try { is = new FileInputStream(fn); dis = new DataInputStream(is); } catch (Exception e) { System.out.println("Fail to Open"); return -1; } int count = 0; node curr = new node(); node last = this; if (curr.readString(dis) < 0) { return -1; } else { ++count; last.setItem(curr); curr = new node(); while (curr.readString(dis) >= 0) // allow 0 length item { last.setNext(curr); last = last.getNext(); curr = new node(); ++count; } } return count; } //readFromFile public static void main(String[] args) { // Takes an command line argument "fn" that is a file name // If command line argument is omitted, "fn" defaults to "input.0" // A list is created from "fn" (one line per list item) // (Empty line is allowed, duplicated list items allowed) // The created list is printed. String fn; node lists; fn = (args.length == 1)? args[0] : "input.0"; lists = new node (); lists.readFromFile(fn); lists.print(); } } //class node