Note that there are some explanatory texts on larger screens.

plurals
  1. POLinked list single class vs multiple classes
    text
    copied!<p>In my second term as an computer science student almost the whole term we have focused on writing linked lists in different variations(stack, queue, ...). The design of these lists always came down to this</p> <pre><code>class List&lt;T&gt; { class ListElement { T value; ListElement next; } ListElement root; } </code></pre> <p>with variations to which methods were implemented and how they worked (I have left out constructors and properties for simplicity here).</p> <p>Some day I started learning scala and focusing on functional programming. This also came to the point where a linked list was written but in a different <em>style</em> of implementation.</p> <pre><code>class List[T]( head: T, tail: List[T]) </code></pre> <p>Despite the different syntax and immutability this is in my opinion a different aproach. And I thought to myself "Well you could have implemented lists the same way in C# or Java with one <code>class</code> less than the aproach you learned".</p> <p>I can see why you would implement a linked list like that in a functional language where recursion is not as <em>dangerous</em> as in C# or Java because at least for my way of thinking a recursive implementation of all the usual methods on a linked list for this design is very intuitive. </p> <p><strong>What I do not understand is why are linked lists in C# or Java typically implemented in the first fashion when you could implement them the other way with less code but equal verbosity?</strong> (I am not talking about the implementation of lists in the libraries of the language but about the lists you typically write as a programmer to be)</p> <p><strike>The only benefit I can see with the first approach is that you can hide the implementation from the user a bit better but is this the reason and also is this worth the additional <code>class</code>?</strike> I wouldn't even need to expose my implementation to the user as I could still implement my list internally different and maybe only have chosen to have a constructor like that and provide functionality to retreive the first element of the list as <code>head</code> and also the rest as <code>tail</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload