What are some advantages of linked lists

What is the benefit of the linked list over an array and vice versa?

Both store a sequence of items, but with different techniques.

One array stores items in sequential order in memory, i.e. it looks like this:

This means that the elements are in memory one after the other.

((Double), bound) liston the other hand, stores the items in the following way: there is a separate "list-item" for each item; the "list-item" holds the current item and a pointer / reference - / note - / etc in the next "list item". When it's double-linked, it also contains a pointer / reference / note / etc to the previous "list-item":

This means the items can be distributed all over the memory and stored in specific storage locations.

Well, we know that we can compare some common operations on sequences of elements:

  • Access to an element at a specific index position: With a array we just calculate that offset for this index and the element at the index.

    This is very cheap. With a list on the other hand, we don't know a prioriwhere the element is stored at the index position (since all elements are somewhere in memory), we have to walk the list element by element until we find the element at index. This is an expensive process.

  • To add a new element at the end of the sequence: With a array This can lead to the following problem: Arrays are usually fixed size, so if we have the situation that our array is already completely filled (see), we probably cant get the new element here because it might already be something other. So, maybe we need to copy the whole array. However, if the array is not filled, we can just put the element there.

    With a listwe need to create a new "list item", put the element in it and insert the pointer of the currently last element of this new list. Usually we store a reference to the current last element so that we don't have to search through the whole list to find it. So, adding a new item is really no problem with lists.

  • Adding a new element somewhere in the middle: consider you first arrays: here, we could be in the following situation: we have an array with elements 1 to 1000:

    Well, we want to insert between and: That means we have to move all Items off to a position to the right to make room for the:

    Moving all of these items is a very expensive operation. So better not to do this too often.

    Now let's consider how a list can do this task: let's say we currently have the following list:

    Again, we want to insert between and. This can be done very easily: let's create a new list item and update the pointers / references:

    We have simply created a new element in the list and created a kind of "bypass" to put it - very cheap (as long as we have a pointer / reference to the list element the new element is inserted after).

So, let's summarize: in connection with Lists are really nice when it comes to inserting them in random positions (as long as you have a pointer to the appropriate list item). If your work involves adding lots of dynamic and traversing elements all Items anyway, a list would be a good choice.

One array is very good when it comes to index traffic. If your application needs access to elements in certain positions very often, you should use an array.

Notable things:

  • Solving the fixed-size problem for arrays: As already mentioned, (raw) arrays are usually fixed-size. However, this problem is no longer a real issue these days, as almost all programming languages ​​provide mechanisms for creating arrays that grow (and possibly shrink) dynamically - exactly as necessary. This growing and shrinking can be implemented so that we can continued Term of O (1) insert and remove elements (at the end of the array) and those that the programmer does not mention and expressly.

  • Since lists give, such nice properties can be used for the insertions, like the underlying data structures for search trees, etc. I. e. Construct a search tree, the lowest level of which consists of the linked list.

Shareimprove this answer phimuemue