0

I was reading a sentence from here and I was confused by a sentence:

"This makes the underlying routines able to be much faster than lists in other languages, where you have to be concerned about double-links, destructive updating, and so on."

I tried Googling for double-links but couldn't find a proper definition. What does the author mean when he says "double-links"?

pyrrhic
  • 1,769
  • 2
  • 15
  • 27
  • lists are data structures where blocks of memory are chained by pointers, also known as links. There are basically two breeds of lists. Some only use one pointer per element (chaining an element to its successor), some use two pointers (chaining each element to both its predecessor and successor). These are often called "double-linked lists". – kuroi neko Jan 11 '14 at 22:26
  • 2
    You're missing the context: "For example, *since [functional] lists are immutable* .. you have to create a new list." With that in mind, the "double-linked" term used here refers to a [*doubly-linked list*](http://en.wikipedia.org/wiki/Doubly_linked_list), which is not needed (and generally not possible) for an immutable list type as found in languages like ML or Haskell. Anyway, summarizing: *using immutable lists makes correct usage simpler* (because there are factors that simply aren't present). – user2864740 Jan 11 '14 at 22:27
  • The author seems to be using "double-links" in a buggy sense, though. Is it the case that single-link is faster than double-link, because of the overhead used by the second pointer? – pyrrhic Jan 11 '14 at 22:27
  • @pyrrhic Not at all. The author was saying that some issues associated with DLLs are simply not present in immutable [SL]L designs. Of course, [SL]Ls are forward-only traversal, but that's another point (that does affect how code is written). – user2864740 Jan 11 '14 at 22:28
  • @user2864740 I'm a bit confused, because the sentence seems to be directly talking about speed "i.e. much faster than lists on other languages..." What is it exactly about the single link that makes it faster than the double link? – pyrrhic Jan 11 '14 at 22:29
  • 1
    @pyrrhic That last bit about speed is indeed hyperbolic and could have been written better. Different algorithms will indeed favor one approach over another so talking about "speed" is a bit silly. However, I believe that the author is making a pointing that the simpler data-structure requires less maintenance (because it can't be mutated) and that the immutable design avoids destructive-updates/copies (because it can't be mutated). Thus, *when* appropriate for the algorithm (e.g. front-append and forward-traversal) an immutable list can avoid overhead associated with mutable DLL approaches. – user2864740 Jan 11 '14 at 22:45

1 Answers1

1

Before answering we need to get some definitions and concepts down.

In the context of the referenced article are two concepts of list.

  1. Immutable list as used in functional languages such as ML.
  2. Linked list, (mutable list) as used in imperative languages such as C, C++, Java.

Also the way the list are constructed, destroyed and used are different.

While each list must be constructed, immutable list are constructed by only adding new items to the head of the list while linked list can be constructed by adding items anywhere in the list, though typically new items are added to the end of a linked list.

Immutable list are only traversed in only one direction, forward from head to tail, while linked list can be traversed in both forward and backward direction and can even change direction while traversing.

Immutable list are not destroyed via statements in the source code but are automatically destroyed via a garbage collection, while linked list must be destroyed with a destructor statement.

Additionally immutable list are all of the same type, while linked list can hold items of varying types.

Because of the limitations of immutable list, an operation on any item is O(1). For example to add an item to a list is O(1) because it is always added to the head. To use an item in a list is O(1) because it is the current head of the list. How this is accomplished is that when a list is used with in a functional language, the head item is used and the tail of the list is passed on so that the tail of the list being passed on has a new head. i.e. for the list 1,2,3 we use the head of the list 1, and then pass on the list 2,3 where 2 is the new head. Yes in concept they are two different list but the details of how this is implemented is another question.

What does the author mean when he says "double-links"?

"double-links" is referring to doubly linked list which are list in languages that allow the construction of items in the list to have pointers to both the preceding and succeeding item.

The OP in the comments also ask:

What is it exactly about the single link that makes it faster than the double link?

As I noted in the earlier section, it is that all operations on list are O(1).

Guy Coder
  • 24,501
  • 8
  • 71
  • 136