You are correct. Accessing an index in the dynamic array takes O(1) time complexity. Which basically means it takes constant time, dependent on how fast the computer can access the data. On there other hand, to access a specific node in a linked list you must loop through the entire list, which take O(N) time. N represents how many inputs you have.
Obviously dynamic arrays are better for this application. But Linked lists are better for others, such as inserting or removing at any point in the list.
This is why it is important to understand what sort of operations will be run on a set of data and choose the best data structure to represent that in a program.
> Is there any way to test this?
Using <chrono> or clock_t.
Test this with a very large number of objects (say, one hundred million objects) and see the results.
Thank you so much for your insightful replies
"Obviously dynamic arrays are better for this application. But Linked lists are better for others, such as inserting or removing at any point in the list."
I want to share that even though common manipulation of data mainly includes addition, subtraction, movement and retrieval. I have been coding in C for about a year now and from my experience retrieval of data dominates the main while loop so to speak. And if you think about it, dynamic arrays actually overtake linked lists in performance when it comes to addition and subtraction as the size of of the list increases.
With regards to rearranging the node positions for linked lists first you need "n" cycles to find the hooks which hold old and new data and then hook up at least 8 pointers assuming this is a doubly linked list and both the nodes are somewhere in the middle. Where as with a dynamic array you just need
0(2) time complexity and 3 pointer hook ups.
There is also the benefit of having to use 8 bytes less per data structure since you don't need the left and right pointers. I know these things seem really small but I think they add up, or maybe I'm just a hobbyist and I care too much about my code.
Call me biased but I think dynamic arrays outweigh linked lists regardless of the routines being performed.