Array vs List vs Linked list

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.

No alt text provided for this image





3) Linked list

No alt text provided for this image

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

No alt text provided for this image

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;?


?????????????????

回复
Islam Hegab

Software Developer @ Track Int'l Trade

3 年
回复
Islam Hegab

Software Developer @ Track Int'l Trade

3 年

????

Inji Muhammed

Owner and founder of Inergy Co-Working Space

3 年

Very useful information ??????

Asmaa Kamel

Architect and Interior Designer

3 年

I am so proud ????????????????????

要查看或添加评论,请登录

Nourhan Mohamed Saleh的更多文章

  • DDOS Attacks

    DDOS Attacks

    A distributed denial-of-service (DDOS) attack is a malicious attempt to disrupt the normal traffic of a targeted…

    1 条评论
  • OAUTH and tokens

    OAUTH and tokens

    Using APIs for communications between different applications and sharing resources has been a main aspect in our age…

  • nodejs_concurrency

    nodejs_concurrency

    Synchronous code in simple words means : Executing code in order . Asynchronous code : Executing code concurrently (at…

    3 条评论
  • Connection Pool

    Connection Pool

    On dealing with databases we always face that matter of having a connection limitation on each database server. Solving…

    3 条评论

社区洞察

其他会员也浏览了