简体   繁体   English

搜索链接列表和数组时有什么区别?

[英]what is the difference between linked list and array when search through them?

I understand that array's value can be accessed directly by their position and linked list have to go through them one by one but have no idea of how to explain the difference in terms of their overhead and storage when the search is happening. 我了解可以通过数组的位置直接访问数组的值,并且链接列表必须一一遍历它们,但是不知道如何在搜索发生时解释它们在开销和存储方面的区别。

( I am think more in terms of does the previous node need to temporary store somewhere while try to access the next node any additional storage or overhead on the system part when go through them? and same when search through an array) (我认为更多的是前一个节点是否需要在某个地方临时存储,同时尝试访问下一个节点时系统部分在系统部分上的任何其他存储或开销?在搜索数组时也是如此)

Can anyone give me a detail of what happen when search in each structure? 谁能给我详细介绍在每个结构中进行搜索时会发生什么? or simply point to a right direction 或只是指向正确的方向

An array is a vectorial variable of a fixed size. 数组是固定大小的矢量变量。

A Linked List has no specified size: each element of the list contains a pointer to the next element. 链接列表没有指定的大小:列表的每个元素都包含指向下一个元素的指针。 That's why you need to iterate through it sequentially. 这就是为什么您需要按顺序遍历它的原因。 The advantage here, is that the structure shall not be allocated in a sequential block of memory, and doesn't need to resize if you add more elements in it. 这样做的好处是,该结构不会在内存的连续块中分配,并且如果在其中添加更多元素,则无需调整大小。

Also in an array, if you remove an element, you need to shift all previous elements. 同样在数组中,如果删除元素,则需要移动所有先前的元素。 If you insert an element in the middle of an array, you need to shift elements to make space for the new one. 如果在数组中间插入一个元素,则需要移动元素以为新元素腾出空间。 In lists you just update the pointers: 在列表中,您只需更新指针:

在此处输入图片说明

On the other side, array can be accessed randomly and don't need sequential access: so they are faster to search for objects, to sort, etc. 另一方面,数组可以随机访问,不需要顺序访问:因此它们可以更快地搜索对象,进行排序等。

拥有对列表元素的随机访问权限,可以实现二进制搜索之类的搜索算法,而使用链接列表则不可行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM