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

前端 未结 6 2012
南旧
南旧 2021-01-30 18:12

Please explain what is the advantage of linked list over an array. And also is there any advantage of using array compared to linked list.

Regards, Shoaib

6条回答
  •  隐瞒了意图╮
    2021-01-30 18:35

    If you don't know the amount of objects you need to store beforehand, a list is probably what you want, since it's very easy to dynamically shrink or grow the list as needed. With this also comes the advantage of being able to easily insert elements mid-list without any need for reallocation.

    The disadvantage of a list vis-à-vis an array, on the other hand, is that it's slower to select individual elements, since you need to iterate. With an array, you won't have this problem. Arrays, on the other hand, are troublesome to use if you need to resize it, as this operation is more costly than adding or subtracting elements from a linked list.

    Lists should be used more commonly, since the ease of use is often more beneficial than the small performance gain from using a static size array.

提交回复
热议问题