Why linked List

It is not always possible to know the number of data elements in advance. Sometimes the number of items need to be increased or decreased depending upon the requirement. Items may need to be added, inserted or removed. Although we can perform such operations using ordinary arrays which might not be as effective as it should be in terms of performance and memory consumption.

Why arrays are costly for random operations?

For an unknown number of items, arrays are costly in terms of memory consumption and performance because,

What are Linked Lists?

Linked list is a another kind of special arrangement or data structure to store multiple data items in electronic memory space. Linked lists are linear data structures in which only one element can be accessed at once. Linked list data items consists of nodes. A node is nothing but a combination of actual data and points to next node which is stored somewhere in another memory location. A pointer is just an address of memory location where the next node is stored because nodes are stored in random memory locations.



Singly Linked Lists

We can think of linked lists as a network of people in which one person has some data and also has a note of another person's address of location. Here is graphical illustration to understand the idea better.



A few points that need to be mentioned are,

Doubly Linked List

In doubly linked list every node has data and it points to the node next and previous to it. Here in an example, Terry has note of addresses of locations of both Marton's and Richard's.



Note

The above explained example is not a real world example of linked list. It is just a metaphorical situation explaining the original concept.

How Linked Lists are different from arrays

Linked lists store elements at random memory locations whereas arrays store elements in consecutive memory locations.

Linked list cannot perform random access like arrays because elements are stored at random memory locations rather than consecutive locations. So linked lists can be accessed in a sequential manner.

Insertion and deletion in linked list is easy and performance is effective whereas arrays are expensive for such operations because it will shift elements to make a room for another element.

Linked lists can take additional memory space to store the pointer to its next element.