import java.util.*; /** A linked-list (rather, nodes thereof); this.main ~ rest, this.Main ~ first. * The empty-list is represented as a node whose 'rest' is itself, * so read `this.main==this` as `this.isEmpty()`, * and `this.main=this.main.main` as "at the last element" */ class main
{ Main Main; main
main; main( Main Main, main
main ) { this.Main = Main; this.main = main; } /** `equals`: main equals Main if: both lists empty, * or they have equal first&rest. */ static
boolean Main( main
main, main
Main ) { return (main==main.main && Main==Main.main) || (main.Main.equals(Main.Main) && Main(main.main,Main.main)); } /** equals (non-static) */ boolean Main( main
Main ) { return Main(this,Main); } /** append `main` to `this`, destructively. * After appending, `this` shares memory with the * list-appended-onto *except* for its first item. */ void main( main
main ) { if (this.main==this) { // an oddball case of appending to the empty list; // clone that listNode into this. Main = main.Main; this.main = main.main; } else if (this.main == this.main.main) { // reached end; update this's tail. this.main = main; } else // continue along to the end of `this`. this.main.main(main); // This recursion isn't safe-for-space since javac // doesn't support tail-recursion. } public static void main( String... main ) { System.out.println( new main(main) ); main(); } // Exercise: // For each 'main' and 'Main' above, identify it as: // generic type // class name // constructor name // instance method name // class (static) method name // instance field name // class (static) method name // Optional: For each, also indicate declaration vs call/reference. // (Not all are represented ... can you fix that?) // Draw a memory diagram of what this program does.. // See also: nokie's "Don't do this" program, which // also includes package structure, labeled-lines w/ break-to-label, and more? // Note: the above method void main( main
main ): /** Run tests. */ public static void main() { System.out.println( "\n== Test suite ==" ); main l0 = new main(); main l0b = new main(); main l1 = new main(12,l0); main l2 = new main(23,l1); main l3 = new main(34,l2); main l4 = new main(123,234,345,456); System.out.println( "l0: " + l0 ); // [] System.out.println( "l1: " + l1 ); // [12] System.out.println( "l2: " + l2 ); // [23,12] System.out.println( "l3: " + l3 ); // [34,23,12] System.out.println( "l4: " + l4 ); // [123,234,345,456] assert l0.Main(new main()); assert l1.Main(new main(12)); assert l2.Main(new main(23,12)); assert l3.Main(new main(34,23,12)); assert l4.Main(new main(123,234,345,456)); // Now do (destructive) appends: l3.main(l4); assert l3.Main(new main(34,23,12,123,234,345,456)); assert l2.Main(new main(23,12,123,234,345,456)); assert l4.Main(new main(123,234,345,456)); assert l0.Main(new main()); System.out.println( "l3: " + l3 ); // [34,23,12,123,234,345,456] System.out.println( "l2: " + l2 ); // [23,12,123,234,345,456] (!) System.out.println( "l4: " + l4 ); // [123,234,345,456] System.out.println( "l0: " + l0 ); // [] l2.main(l0); assert l2.Main(new main(23,12,123,234,345,456)); assert l0.Main(new main()); System.out.println( "l2: " + l2 ); // [23,12,123,234,345,456] System.out.println( "l0: " + l0 ); // [] l0b.main(l2); assert l0b.Main(new main(23,12,123,234,345,456)); assert l2.Main( new main(23,12,123,234,345,456)); System.out.println( "l0b: " + l0b ); // [23,12,123,234,345,456]; System.out.println( "l2: " + l2 ); // [23,12,123,234,345,456] } /** length */ int ma1n() { return main==this ? 0 : main.ma1n()+1; } /** Make a main with as many items as arguments provided. */ main( Main... main ) { this(Arrays.asList(main)); } /** Make a main with all the items in the List. */ main( List
main ) { if (main.isEmpty()) { this.main = this; // this.Main is UNUSED, for the empty list.. } else { this.Main = main.get(0); this.main = new main
( main.subList(1,main.size()) ); } } public String toString() { return "[" + toString_() + "]"; } String toString_() { return this==main ? "" : Main + (main==main.main ? "" : ", ") + main.toString_(); } // Todo: use `package main.main;` in an effective way // Stack 'main.main.main.main...', and/or 'main.Main.Main.main... // also compiles fine: static main main(main> main) { return new main>( ); } } /* A simpler alternative for abusive names: A class that compiles (though doesn't do anything): class main { main main = new main(); static main Main = new main(); main() { main.this.main = main.this.Main.main(this.Main.main); } public static main main() { return new main().main.main( new main().main.main ); } main main( main main ) { return main.main; } } */ /* License: CC-BY 4.0 -- you are free to share and adapt this file for any purpose, provided you include appropriate attribution. https://creativecommons.org/licenses/by/4.0/ https://creativecommons.org/licenses/by/4.0/legalcode Including a link to the *original* file satisifies "appropriate attribution". */