DEV Community

Hassan Khan
Hassan Khan

Posted on

Why we should use linkedlist??

As we know that integer type variable covers 4 bytes of memory. So let's suppose we have an integer type of array with size three and also a LinkedList with three nodes.int array[3] will cover 12 bytes of memory and integer type of LinkedList with three nodes will cover 24 bytes of memory because data part of each node will cover 4 bytes and pointer part also covers 4 bytes. So each node will cover 8 bytes of memory. Collectively three nodes will cover 24 bytes of memory while integer type of array with the same size will cover 12 bytes of memory. Now, why we should use LinkedList over an array?

Top comments (2)

Collapse
 
garychan8523 profile image
garychan

sometime you cant acquire a large enough consecutive space

Collapse
 
hassankhan profile image
Hassan Khan

Do you mean that array requires a contiguous block of memory that's why we use linkedlist??