Determining if to Use Linked Lists or Arrays in C Programming

0
209

Both linked lists and arrays can be used in storing linear data of the same type, but each has some advantages and disadvantages over the other.

At what conditions whether to use linked lists or arrays in C Programming

Linked ListsLinked lists are preferred when the size of the arrays needs to be dynamic, that is, when you do not know the number of elements or items that will be in the list. For the arrays, the size of the array must be known in advance and the allocated memory storage is equal to the upper limit on the number of array’s elements.

Linked lists are most preferred over the arrays when there is no need for random access to any of the elements.
Linked lists are preferred where there is need to insert or delete data elements with ease. It is more difficult for you to add new elements in an array. This is because you have to shift the existing elements in order for you to create a room for the elements being inserted. For instance, if you have a sorted list of ages in an array age[]=[23, 31, 37, 43, 56, 75] and you need to insert a new age 29, then you must move all the elements after 23 for you to maintain the sorted order of the list.

ArraysArrays are always preferred when you need random or indexed access to the data elements.

Arrays are used when memory is a concern. When the arrays are filled up, they usually use less memory than the linked lists.

It’s most appropriate to use arrays when you need to fast iterate through all the elements in a sequence. You just need to use the math pointer on the array in order to access each element.

Arrays are preferred over the linked lists whereby you know the number of elements in advance and therefore you are able to make correct memory allocations for the data.

LEAVE A REPLY

Please enter your comment!
Please enter your name here