+ 1

How do linked lists work?

I was wondering what a practical approach to linked lists would look like.

22nd Nov 2016, 8:31 PM
Ruchi Patil
Ruchi Patil - avatar
1 Respuesta
+ 3
in contrary to arrays which allocates a sequential space in the memory, for example, requesting the following: int* arr = new int[3]; will cause the operating system to look for enough free space for the allocation so if the space found at address, let's say for the sake of this example, 100, then since each int occupies 4 bytes, the addresses are as follow: arr[0], arr[1], arr[2] 100, 104, 108 so if you want to allocate a HUGE amount of memory, you need to have a long sequence of free blocks with linked list it is different each node of the linked list contain some data, and a pointer to the address of the next node and so on, with the last node pointing to NULL value to indicate the end of the list. so a similar array with 3 integers could look like this: [data|--]-->[data|--]-->[data|--]-->NULL 100 240 160 as can be observed, the memory is not sequental at all, which can have some benefits, but also suffer from poor access time for instance as to get the 3rd element, you have to go through alllll the linked list, while with the array it will be a simple arr[2] access.
22nd Nov 2016, 9:18 PM
Burey
Burey - avatar