r/learnprogramming • • Jan 31 '14

Can someone ELI5 linked lists?

Part of my assignment for my online class involves linked lists and we don't really go over them. I looked around online and I'm slightly confused by all the explainations since I'm fairly new to programming. Can someone dumb it down for me?

77 Upvotes

85 comments sorted by

View all comments

Show parent comment

4

u/FantasticFourSkin Jan 31 '14

After reading some other comments and getting the jist of linked lists, your comment really helped me to understand them. Thanks!

1

u/door_of_doom Jan 31 '14

If you are interested in it, a fun little challenge for yourself is to put the two data structures to the test and see the results for yourself!

Create an array that is 100,000 units long (they can just be int, whatever, it doesn't matter) and make a linked list of the same size.

Now, do several speed tests. See how long it takes you to insert a new number halfway through both lists. The array will take much, much longer, because it has to physically move half of the list. The linked list just needs to change a few pointers.

But then try a test where you just read the very last unit in both lists. The array will be much, much faster, because it already knows exactly where it is located. It will have to start a scavenger hunt to find the last element of the linked list.

I really recommend that you write a simple program that demonstrates it so that you can see just how dramatic the differences can be! it will really solidify your understanding,

1

u/FantasticFourSkin Jan 31 '14

Would the Big Oh notation be switched for each test? I'm just learning about that too and still trying to understand it.

1

u/subsage Jan 31 '14

I'm not exactly sure what you mean by "switched," but I think you may be correct in what you mean to say. The Big Oh for each is different when it comes to different operations.

I hope I'm not getting ahead here. So when we're looking at data structures, there's usually a reason for why a specific one is used in some situations and others in different situations.

As /u/door_of_doom was hinting at, different data structures are more efficient than others, but there's different ways of being efficient. The biggest factors of efficiency are memory and time (or speed). These can sometimes overlap, but that's for later.

What I think you meant to say with Big Oh is if you'll really get different results from the testing of each data structure. Answer is that yes, you should. You say you're still trying to understand Big Oh, how's that coming along?

1

u/door_of_doom Feb 01 '14

I honestly don't know exactly how the efficiency of each data model is expressed in Big Oh. I just know that I did it back when I was learning about it, and it was crazy to see results that were orders of magnitude different from each other,

1

u/Jonny0Than Feb 01 '14 edited Feb 01 '14

In practice, this is actually wrong:

The array will take much, much longer, because it has to physically move half of the list.

Cache coherency dominates in a lot of unexpected places, and can overcome algorithmic complexity when the unit of work is relatively small.

When you were hopping through half your list to find the right spot to insert, you were missing memory cache all over the place. Finding the middle of an array is trivial. And moving all that data? It's all adjacent so there are very few cache misses, and it's way faster than you'd expect.

I didn't believe this the first time I heard it, but I wrote some test programs and it's actually true.

For an apples-to-apples test, fill an array and a list with integers 0-999,999 and randomly shuffle them. Then pick a number at random, find it, and remove it from the container. When the number is near the beginning of the container, the list will be faster. Last time I tried this the list won when the number was in the first 25% of the container, and the array was faster otherwise.

1

u/door_of_doom Feb 01 '14

Fascinating! What language were you running the test in?