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

16

u/kqr Jan 31 '14

This is a very good mental model. Another disadvantage is that if you find the Oh Henry bar there is no way for you to get back to the Snickers. So in a way, you can only do the scavenger hunt in the intended order, and not backwards. (If you know where the first clue is though, you can make it possible to do it backwards by going to every bar and writing a clue for the previous bar.)

27

u/jamestakesflight Jan 31 '14

but that is going into something that we specifically call a "doubly linked list" and strays from a classic "linked list"

6

u/kqr Jan 31 '14

Yes! It gives you a different data structure, or a variation on the structure you had. Is that a problem, for some reason?

6

u/jamestakesflight Jan 31 '14

no, but the person asking the question doesn't even understand links to begin with, so what you're saying is only posing a problem that the asker of the question could not recognize in the first place. you're just further complicating the principle of a straight up linked list for the asker.

12

u/kqr Jan 31 '14

When I'm tutoring, my students usually like it when I drop clues about further developments. I didn't realise this could be confusing in a non-1-on-1 situation. I'm sorry.

OP, disregard my comment.

15

u/FantasticFourSkin Jan 31 '14

Don't worry! I understand linked, doubly linked, and circular lists now thanks to you guys

6

u/kqr Jan 31 '14

Great! That's what matters the most!

-6

u/[deleted] Jan 31 '14

It is confusing in any situation. Do you know stages of acquiring a skill or knowledge?

2

u/jianadaren1 Feb 01 '14

Yeah, they're extremely parallel.

-2

u/[deleted] Feb 01 '14

here have a look. I hope this can help you in your teaching career.

1

u/DestroyerOfWombs Jan 31 '14

Not really. Once you find the Oh Henry bar, you can return to the first candy bar (head node) and follow the clues until you find the Snickers again. Or, do what the other guy said and use a doubly linked list, where each candy bar has a clue which leads you back to the previous node.

2

u/kqr Jan 31 '14

Granted that you know where the first bar is, which is not always the case.

2

u/door_of_doom Jan 31 '14

Honest question trying to think back to CompSci 101, the only one I ever took:

Assuming you haven't created a memory leak by deleting your leading pointer without deconstructing your linked list, when wouldn't you have access to the address of the first node?

4

u/kqr Jan 31 '14

If you pass any node except the initial to a method, that method doesn't have access to the "leading pointer" even though it exists somewhere in memory.

2

u/door_of_doom Jan 31 '14

Ah, good point! Forgot to consider scope.

2

u/dvlsg Feb 01 '14

Right, but if you're putting together a function that needs the middle node and the first node, it would be silly not to pass a reference to both.

3

u/DestroyerOfWombs Jan 31 '14

If you don't know where the head node is, you don't know where your list is. In a singly linked list, if you don't have a head node you have lost all nodes before the one you do know the location of.

1

u/kqr Jan 31 '14

Sure, some part of the program knows where the head is, but not necessarily all parts of the program. Some parts might only know about a few elements.

-1

u/The_Petunia Jan 31 '14

Or writing a clue to the previous bar every time you add a new candy bar to the hunt.