BlogsDope image BlogsDope

Array vs Linked list in C

May 25, 2017 C LINKED LIST ARRAY DATA STRUCUTRE 3614

Previous:

  1. Linked lists in C (Singly linked list)
  2. Linked list traversal using while loop and recursion
  3. Concatenating two linked lists in C
  4. Inserting a new node in a linked list in C
  5. Deletion of a given node from a linked list in C

In the previous 5 articles, you have covered very much of a singly linked list. Let’s compare the linked lists and arrays and find out the difference in between the two of them.

Let’s first talk about the size. The of an array is fixed but the size if a linked list is dynamic. This means that we can always add more nodes whenever we want in a linked list but this is not possible with an array. We specify the size of an array during the creation of the array.

The second thing is the time required to access the elements. In this case, an array is better, We can directly access any element in an array if we have the index of it but we need to traverse over the entire list in the case of a linked list.

The third thing which should be compared is inserting an element a linked list and an array. To insert an element in an array, we create an entirely new array with different size and then put the elements in the new array. This is indeed more time taking methods than insertion of an element in a linked list.

Similar to insertion, deletion is also more time taking process in the case of an array.

Shuffling the elements of a linked list requires less time than the time required to shuffle the elements of an array because we just have to change the pointer in the case of a linked list but it is more complicated in the case of an array.

We can also compare the space required for an array and a linked list storing the same elements. The space required in the case of an array is less because we just store the data in an array but we also store a pointer in the case of a linked list.

Property Arrays Linked list
Size Fixed Dynamic
Access Fast Slow
Insertion Slow Fast
Deletion Slow Fast
Space req. Less More

 


Liked the post?
Developer and founder of CodesDope.
Editor's Picks
0 COMMENT

Please login to view or add comment(s).