{"id":184,"date":"2010-12-15T20:32:11","date_gmt":"2010-12-15T19:32:11","guid":{"rendered":"http:\/\/www.xenonique.co.uk\/blog\/?p=184"},"modified":"2010-12-15T20:43:24","modified_gmt":"2010-12-15T19:43:24","slug":"reversing-a-singularly-linked-list","status":"publish","type":"post","link":"https:\/\/www.xenonique.co.uk\/blog\/2010\/12\/15\/reversing-a-singularly-linked-list\/","title":{"rendered":"Scala: Reversing A Singularly Linked List"},"content":{"rendered":"<p>Hello Everyone<\/p>\n<p>Here is a Scala application example that demonstrate how to reverse a singularly linked list.<\/p>\n<pre class=\"brush: scala; title: ; notranslate\" title=\"\">\r\n\/* Written by Peter Pilgrim 10th December 2010 *\/\r\npackage uk.co.xenonique.scalaexercises\r\n\r\nimport scala.collection.mutable.StringBuilder\r\nimport java.lang.System\r\n\r\nabstract class LinkedList { }\r\n\r\ncase object EmptyLinked extends LinkedList\r\n\r\ncase class LinkedNode( var value: Int, var next:LinkedList ) extends LinkedList\r\n\r\n\r\n\/**\r\n * Example Scala program that demonstrates an algorithm to reverse a singularly linked list of elements.\r\n *\/\r\n\r\nobject NodeApp {\r\n\r\n  def traverse( node: LinkedList ): String = {\r\n    def _traverse( node: LinkedList, buf: StringBuilder ): Unit = {\r\n      node match {\r\n        case b: LinkedNode =&gt; {\r\n            buf.append( &quot;%d, &quot;.format( b.value ))\r\n            _traverse( b.next, buf )\r\n        }\r\n        case _ =&gt;\r\n      }\r\n    }\r\n\r\n    val buf = new StringBuilder(&quot;[&quot;);\r\n    _traverse( node, buf );\r\n    buf.append(&quot;]&quot;)\r\n    buf.toString\r\n  }\r\n\r\n  def reverse( current: LinkedList ): LinkedList = {\r\n    def _reverse( current: LinkedList, parent: LinkedList ): LinkedList = {\r\n      println(&quot;::&gt;&gt; current=&quot;+current+&quot;, parent=&quot;+parent)\r\n      current match {\r\n        case b: LinkedNode =&gt; {\r\n           val revHead = _reverse( b.next, current )\r\n           printf( &quot;&lt;&lt;:: b.value=%d, b.next=0x%-8X, \r\n                  current=0x%-8X, parent=0x%-8X\\n&quot;,\r\n                  b.value, \r\n                  System.identityHashCode(b.next),\r\n                  System.identityHashCode(current), \r\n                  System.identityHashCode(parent) )\r\n           b.next = parent\r\n           revHead\r\n        }\r\n        case _ =&gt; parent\r\n      }\r\n    }\r\n    \r\n    _reverse( current, null)\r\n  }\r\n\r\n  def main( args: Array[String]) {\r\n\r\n    val s = LinkedNode( 1, \r\n               LinkedNode( 2, \r\n                LinkedNode(3, \r\n                  LinkedNode( 4, EmptyLinked ))))\r\n    print(&quot;s=&quot;+traverse(s))\r\n    val t = reverse(s)\r\n    print(&quot;t=&quot;+traverse(t))\r\n\r\n  }\r\n}\r\n\r\n<\/pre>\n<p>&#160; <\/p>\n<p>The example program above demonstrates the following salient points:<\/p>\n<ul>\n<li>Scala\u2019s syntax for use of <strong>case instance classes<\/strong> that reduce the boiler plate and add syntax sugar to ordinary object classes. (<font color=\"#ccb400\">LinkedNode<\/font>) <\/li>\n<li>One can declare <strong>case object class<\/strong> (<font color=\"#ccb400\">EmptyNode<\/font>) <\/li>\n<li>Case classes can extend other classes or traits or other case classes<\/li>\n<li>Scala&#8217;s ability to nest a function declaration inside another function declaration<\/li>\n<li>a recursive traversal algorithm to traverse a singularly linked node data structure <\/li>\n<li>a recursive algorithm to reverse the sequence of elements in the singularly linked node data structure <\/li>\n<\/ul>\n<p>Merry Xmas <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello Everyone Here is a Scala application example that demonstrate how to reverse a singularly linked list. &#160; The example program above demonstrates the following salient points: Scala\u2019s syntax for use of case instance classes that reduce the boiler plate and add syntax sugar to ordinary object classes. (LinkedNode) One can declare case object class [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[36,37,38,39,40,6],"tags":[],"_links":{"self":[{"href":"https:\/\/www.xenonique.co.uk\/blog\/wp-json\/wp\/v2\/posts\/184"}],"collection":[{"href":"https:\/\/www.xenonique.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xenonique.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xenonique.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xenonique.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=184"}],"version-history":[{"count":5,"href":"https:\/\/www.xenonique.co.uk\/blog\/wp-json\/wp\/v2\/posts\/184\/revisions"}],"predecessor-version":[{"id":185,"href":"https:\/\/www.xenonique.co.uk\/blog\/wp-json\/wp\/v2\/posts\/184\/revisions\/185"}],"wp:attachment":[{"href":"https:\/\/www.xenonique.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=184"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xenonique.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=184"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xenonique.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}