LinkedList vs List<T> [duplicate]

泪湿孤枕 提交于 2021-02-10 18:49:30

问题


Possible Duplicate:
When should I use a List vs a LinkedList

If I expect not to use the access by index for my data structure how much do I save by using LinkedList over List ? If if am not 100% sure I will never use access by index, I would like to know the difference.

Suppose I have N instances. inserting and removing in a LinkedList will only be a o(1) op , where as in List it may me be O(n), but since it it optimized, it would be nice to know what the difference is for some values of n. say N = 1,000,000 and N = 1,000,000,000


回答1:


List<T> is just a wrapper over an Array. LinkedList<T> is only at it's most efficient if you are accessing sequential data (either forwards or backwards).

Linked lists provide very fast insertion or deletion of a list member. Each member in a linked list contains a pointer to the next member in the list so to insert a member at position i:

update the pointer in member i-1 to point to the new member
set the pointer in the new member to point to member i

Check this: When should I use a List vs a LinkedList




回答2:


LinkedList<T> is useful if you perform many random insertions and deletions of items in your list. Otherwise a List<T> is probably the best choice as it carries no overhead for linking the elements in the list (and also can be indexed).

However, if you are concerned about performance you really need to test your actual code.



来源:https://stackoverflow.com/questions/5870887/linkedlist-vs-listt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!