/* file: myNode.java * Basic Algorithms, Fall 1999, Yap * * HW3 problem: * This file contains abstract classes that * you need to fill in with the appropriate body. * First remove all occurrences of the word "abstract" * or any "dummy code" that I may have put in. * * It declares an interface called "nodeInt" * and a class "myNode" that implements nodeInt. * The interface nodeInt is a variation taken from hw2. * */ import java.io.*; // to use DataInputStream //================================================== // nodeInt: // this interface is a slight variation of what // you did for hw2. In particular, we now assume // "item" is a String rather than a general object. // // For comparisons, you use "S1.compareTo(S2)" as // explained under "merge" below. //================================================== interface nodeInt { //================================================== // basic methods (as in hw2) //================================================== public void setItem(String i); public void setNext(nodeInt n); // next = n public nodeInt getNext(); public String getItem(); public void printItem(); public void print(); //================================================== // methods for File I/O (as in hw2) //================================================== public int readString(DataInputStream dis); // NOTE: on Unix/Linux, end-of-line is indicated by "\n" // On Windows, end-of-line is indicated by "\r\n". public int readFromFile(String fn); } // nodeInt public abstract class myNode implements nodeInt { //================================================== // variables //================================================== private String item; private nodeInt next; //================================================== // constructors //================================================== myNode() {// fill in your code; } myNode(String e, nodeInt n) {// fill in your code; } //================================================== // basic methods // -- remove "abstract" and fill in code //================================================== public abstract void setItem(String i); public abstract void setItem(nodeInt n); public abstract void setNext(nodeInt n); public abstract nodeInt getNext(); public abstract String getItem(); public abstract void printItem(); public abstract void print(); public abstract int readString(DataInputStream dis); public abstract int readFromFile(String fn); //================================================== // sort: // implements merge sort of a list of strings // in increasing order. // Returns the nodeInt that is the start of the // sorted list. //================================================== public abstract nodeInt sort(); //================================================== // split: // takes *this* list, and splits it into // an odd sublist and an even sublists. // The value of *this* is the first node in the odd // and the returned value is the first node of the // even sublist. [Note: the even sublist has length // equal to, or one less than, the length of odd sublist.] // // Assume *this* has at least one node. // CAREFUL: the input list must be NULL terminated. //================================================== public abstract nodeInt split(); //================================================== // merge(L, L1) // -- assumes L and L1 not null // -- returns the first node of the merged list. // -- To compare, you will need to use the "compareTo" // method for Strings. E.g. // // String s1 = "111"; // String s2 = "222"; // if (s1.compareTo(s2) < 0) // System.out.println("s1 is smaller than s2"); // else // System.out.println("s1 is not smaller than s2"); // // -- In fact, (s1.compareTo(s2) == 0) if and only if // the two strings are equal. //================================================== public static myNode merge(nodeInt L, nodeInt L1) { // dummy implementation ! return (myNode)L1; }; //================================================== public static void main(String[] args) { /* THIS IS THE REQUIRED BODY: String fn = "input.0"; // default file if (args.length > 0) fn = args[0]; // if file is specified myNode N = new myNode(); int n = N.readFromFile(fn); // create a list from file "fn" System.out.println("....INPUT LIST:"); N.print(); N = (myNode)N.sort(); System.out.println("....SORTED LIST:"); N.print(); */ } } //Node class