Array vs List vs Linked list
I bet a lot have been using a list as an array ! let's discuss the difference between them.
1) Array
An array is?a collection of items (having same data type) stored at contiguous memory locations.
2) List
List is a dynamic array of pointers( objects) , where each index points to the item inside the memory which can be of any data type.
Therefore list items can be saved in any place inside the memory.
3) Linked list
Linked list is a data structure where each item has a value and a pointer to the next item , Therefore linked list items can be saved in any place inside the memory.
When to use array ?
Array is good for indexing items with same data types , It's faster than list as elements are saved inside it at contiguous locations , and consumes less memory as it doesn't need pointers .
领英推荐
When to use list ?
List is good for indexing while dealing with different data types ,
It finds any element by index with complexity O(1). While it has higher complexity in inserting a new item from the top or middle index as it needs to shift other elements . O(n)
When to use linked list ?
Linked list is better used when you need to add a new items on the top or the end O(1). While it's not recommended for indexing O(n) .
List vs array in python
In Python list is implemented by default numbers = [1,2,"three"]
and array can be used by importing a module import array sample_array = array.array('i', [1, 2, 3])?
List vs array in nodejs
Everything in JavaScript is an object, therefore an array in js is an array of objects (pointers to items inside the heap) .
which means an array in js is a list by definition . const?numbers = [1,?2,?"three"];
List vs array in php
Everything in php is a hash map, therefore an array in php is a hash map that allows for int and string keys.
$numbers =?array(1,?2,?"three");
So what is list in php ? List in php is used for different purpose ,It's used to assign array values to variables?: $array = array(1, 2, 3, 4) list($a, $b, $c) = $array;?
--
2 年?????????????????
Software Developer @ Track Int'l Trade
3 年Ahmed Omar
Software Developer @ Track Int'l Trade
3 年????
Owner and founder of Inergy Co-Working Space
3 年Very useful information ??????
Architect and Interior Designer
3 年I am so proud ????????????????????