Linked List vs Array
Both Arrays and Linked List can be used to store linear data of similar types, but they both have some advantages and disadvantages over each other.
Following are the points in favour of Linked Lists.
(1) The size of the arrays is fixed: So we must know the upper limit on the number of elements in advance. Also, generally, the allocated memory is equal to the upper limit irrespective of the usage, and in practical uses, upper limit is rarely reached.
(2) Inserting a new element in an array of elements is expensive, because room has to be created for the new elements and to create room existing elements have to shifted.
For example, suppose we maintain a sorted list of IDs in an array id[].
id[] = [1000, 1010, 1050, 2000, 2040, .....].
And if we want to insert a new ID 1005, then to maintain the sorted order, we have to move all the elements after 1000 (excluding 1000).
Deletion is also expensive with arrays until unless some special techniques are used. For example, to delete 1010 in id[], everything after 1010 has to be moved.
So Linked list provides following two advantages over arrays
1) Dynamic size
2) Ease of insertion/deletion
Linked lists have following drawbacks:
1) Random access is not allowed. We have to access elements sequentially starting from the first node. So we cannot do binary search with linked lists.
2) Extra memory space for a pointer is required with each element of the list.
3) Arrays have better cache locality that can make a pretty big difference in performance.
Please also see thisthread.
References:
http://cslibrary.stanford.edu/103/LinkedListBasics.pdf
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Linked List vs Array的更多相关文章
- 关于Android中ArrayMap/SparseArray比HashMap性能好的深入研究
由于网上有朋友对于这个问题已经有了很详细的研究,所以我就不班门弄斧了: 转载于:http://android-performance.com/android/2014/02/10/android-sp ...
- ArryList vs LinkedList
references: http://www.javaperformancetuning.com/articles/randomaccess.shtml http://stackoverflow.co ...
- The C5 Generic Collection Library for C# and CLI
The C5 Generic Collection Library for C# and CLI https://github.com/sestoft/C5/ The C5 Generic Colle ...
- 给Lisp程序员的Python简介
给Lisp程序员的Python简介 作者:Peter Norvig,译者:jineslong<zzljlu@gmail.com> 这是一篇为Lisp程序员写的Python简介(一些Pyth ...
- amazon interview
I'll be sitting for an Amazon interview in 3 months. Which website should I use to practice: SPOJ, H ...
- [004] last_k_node
[Description] find the k-th node from the last node of single linked list. e.g. Linked-list: 1-2-3-4 ...
- 面试题——ArrayList和LinkedList的区别
List概括 先回顾一下List在Collection的框架图: 从图中可以看出: List是一个接口,他继承Collection接口,代表有序的队列. AbstractList是一个抽象类, ,它继 ...
- Array vs Linked List
Access: Random / Sequential 1. Array element can be randomly accessed using index 2. Random access f ...
- Lintcode489-Convert Array List to Linked List-Easy
489. Convert Array List to Linked List Convert an array list to a linked list. Example Example 1: In ...
随机推荐
- js 字符串“123”,变成整数123,不用parseInt 函数
var s = "123"; console.log(s.charAt(0)*100+s.charAt(1)*10+s.charAt(2)*1);
- 使用ANT 生成Xfire 客户端端文件
这里需要用到的JAR包 : XmlSchema-1.1.jar activation-1.1.jar commons-codec-1.3.jar commons-httpclient-3.0.jar ...
- PHP弱类型安全问题笔记
一.类型转换问题 intval(); var_dump(intval('1asdfasd')); //1 var_dump(intval('awqw12')); //0 var_dump(i ...
- Python开发【第一篇】Python基础之自定义模块和内置模块
为什么要有模块,将代码归类.模块,用一砣代码实现了某个功能的代码集合. Python中叫模块,其他语言叫类库. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代 ...
- Oracle数据库的创建、数据导入导出
如何结合Sql脚本和PL/SQL Developer工具来实现创建表空间.创建数据库.备份数据库.还原数据库等操作,然后实现Oracle对象创建.导入数据等操作,方便我们快速了解.创建所需要的部署Sq ...
- [Environment Build] 如何实现Visual Studio中的区域语言环境切换
最开始学习C#这门语言的时候,英文能力不够好,安装的中文版本的Visual Studio,现在工作有段时间了,公司又是个外企,慢慢不习惯中文版本的了,于是产生了想切换语言的想法,网上搜索了下,下载了个 ...
- 快书包CEO徐智明反思:我犯下哪些错误
新浪科技 刘璨 1月23日,快书包CEO徐智明在微博上公开“叫卖”快书包,在业内引起不小反响.这家创立于2010年要做“网上711”的创业公司,曾以独特的“一小时送达”服务在业内成为关注焦点. “如果 ...
- Linux进程操作信息
Linux进程操作简单小结 linux上进程有5种状态: 1. 运行(正在运行或在运行队列中等待) 2. 中断(休眠中, 受阻, 在等待某个条件的形成或接受到信号) 3. 不可中断(收到信号不唤醒和不 ...
- Java加解密与数字签名
** Java加解密 ** 实现方式:JDK实现,CC,BC JDK提供比较基础的底层的实现:CC提供一些简化的操作:BC提供补充 一.Base64加密 非常简单,加密解密就一个函数. 代码如下: 二 ...
- ListIterator-迭代器
Iterable(lang)-->Iterator(util)-->ListIterator(util) method: 1.void add(E o) 2.boolean hasNext ...