r/learnprogramming • • Apr 05 '22

Name one programming/comp sci concept you never understood and if you understand it, try to explain it to them

Name a programming concept such as virtual functions, pointers, certain algorithms, etc that you always had a hard time understanding and why.

Someone else who reads yours and does understand it, try to explain it to them to help them learn

1.6k Upvotes

738 comments sorted by

View all comments

14

u/[deleted] Apr 05 '22

linked lists bruh i cannot get it rn

2

u/tastes-like-chicken Apr 06 '22 edited Apr 06 '22

Let's start with pointers, they're fundamental to the concept of a linked list. A pointer "points" to some location in memory. It isn't actually the thing that it points to, but it holds the memory address of that thing. Pointers can get a bit complicated as you read more about them but just remember that they point to a memory address where something is located, and you can access that something by dereferencing the pointer.

Now we'll talk about arrays, since it will help you understand why linked list are useful. Arrays are stored contiguously in memory. This means that if you have an array with 5 elements, you need 5 blocks of memory that are side-by-side to store that array. More info on that can be found here if you need more explanation. Just remember that arrays are stored contiguously, so they require a chunk of memory large enough to store each element side-by-side.

What if we don't want to store our data contiguously? Enter linked lists. Linked lists are like arrays in that they store a "list" of data. Unlike arrays, the elements in a linked list can be stored non-contiguously, meaning they don't have to be side-by-side in memory. This is because of pointers. Each element in a linked list, referred to as a Node, stores a piece of data and a pointer to the next element. That way, we can "connect" all of the elements in the list, so that you can get from the first element, to the second, and so on.

Why not just use an array? One of the tricky things about (non-dynamic) arrays is that it's expensive to delete elements, insert elements into the middle of the array, or resize the array because you essentially need to copy the old elements into a new array or write over the existing elements. With linked lists, deleting nodes only requires removing data and rearranging pointers. You also don't ever need to resize a linked list, as nodes can be added anywhere in the list by rearranging pointers. The downside is that you can't access a particular element as quickly as an array, because you have to traverse through each element starting at the head (or tail) node. Also inserting at the end of the list is expensive as you have to traverse the entire list, while inserting to the end of an array happens in constant time.

How are linked lists implemented? There are many ways, but typically you would create a Node object using a struct or class, and that object would hold one variable for the data and one Node* pointer to point to the next node. The list itself could be another class that contains a head Node, which is used to access all other elements in the list.

Hopefully that helps!

This comment expands a bit more into the topic

-2

u/ShelZuuz Apr 06 '22

You will one day.

And then soon after you do, you'll learn never to use them in practice... there are outdated with modern day memory controller architecture.

1

u/warlaan Apr 06 '22

Linked lists in the strictest sense yes, but the concept is not. For example if you are working on a potentially huge data set you would want an array for performance reasons, but with an array you would need to know in advance how large it must be.

If you have no way of knowing the size in advance you have a problem. You could use a vector (i.e. an array that is resized when it becomes too small), but if the data set is so large that you can't keep all of it in memory at the same time you still have a problem.

One easy solution is to use something in between linked lists and arrays by simply creating "liked arrays". You start with an array of a predefined size, and when it is filled up you append a link to a second array of the same size. That way you have (almost) the performance of arrays because you rarely need to make those "linked list jumps", but you also don't have the performance overhead of having to copy huge arrays when resizing that container, because now you can simple remove whole chunks from the linked list part.

1

u/ShelZuuz Apr 06 '22

Unless you have esoterically large elements, a linked list will take up more than twice the amount of space as an array in memory.

Pointer extra per node as well as alignment and minimum allocation requirements from the Heap Manager.

If you can store a linked list you can store, grow and shrink a vector in the same amount of memory, and it will outperform the linked list in virtually every other way.

1

u/warlaan Apr 06 '22

I am not sure if you even read what I wrote. I am specifically talking about linking large arrays.

1

u/ShelZuuz Apr 06 '22

I am not sure if you even read what I wrote. I am specifically talking about linking large arrays.

I did. That's called a deque (in C++ at least), but in the sole exception of a queue that needs double-ended insertion, the benefits of a deque does not generally outweigh the drawbacks. But either way, in that one scenario a deque definitely has a place - I'll grant you that.

However, you started by saying: "If you have no way of knowing the size in advance you have a problem". That's not actually a problem when just comparing a list to a vector.

Adding an unknown number items to the end of a list vs. a vector, inserting into the vector is going to keep doubling the size of the vector when it gets full and then copy over all of the elements to the new allocation. That is still order of magnitudes faster than inserting into the end of a list. And once you're done inserting, you can copy the vector to another vector that's the exact right size you want. With all that, the whole process will still be 10 times faster than inserting into the end of a list, and at no time in between will the vector (or two times the size of the vector if you include the temporary to make a copy), take up more space than the list.

The only consistent advantage a list has over a vector is double-ended insert/delete. (But the deque is better at it again).

However, when you look at code from juniors in a codebase, you'll see std::list all over the place rather than std::vector because they just learned the basics of it in Computer Science class with O-theory behind it and they think that inserting into a list actually is faster or takes less space than inserting into a vector. They're not doing it because they are writing queues that need actual double ended insertion.

I mean lists has to be taught because it forms the basis of binary trees, but I wish after that people will forget about them and stop attacking my codebase with std::list all over in critical perf paths that I have to go fix years later.

Speaking of binary trees... they're also only better if you have a binary tree that's very often being written to after initial construction. If you have a binary tree that's predominantly read from, you're again better off inserting into a vector, doing a sort afterwards and searching it with standard binary search. My ultimate pet peeve is when people get already ordered data from SQL inside a vector, and then go store it into a binary tree for cached access. Like ... duuude.

1

u/warlaan Apr 06 '22 edited Apr 06 '22

I don't quite see why, but you have misunderstood me and that has led you to believe that we disagree. We don't. Sorry if it's my fault, I am not a native speaker.

Also as you will probably agree situations where a one-dimensional linked-list-like container is actually a good solution are a bit hard to come up with.

By the way one reason why you are confronted with people overusing std::list and std::map may be that in C# the vector is called List and the hash map is called Dictionary (as opposed to SortedDictionary, which is the binary tree - so in C# the "vanilla" map is the unsorted one whereas in C++ it's the binary tree one).

1

u/ShelZuuz Apr 06 '22

I don't quite see why, but you have misunderstood me and that has led you to believe that we disagree. We don't. Sorry if it's my fault, I am not a native speaker.

I don't believe we're disagreeing. This is an open forum discussion and you and I are obviously both seniors debating on a sub that's mostly for junior people, so it's more for the benefit of other people that I'm being that verbose. I'm not really disagreeing with you specifically.

I don't think it's language (C#/C++) confusion - I don't generally hire C# devs - always been a C++ shop. It's either UI devs that don't worry about perf cause they have a zillion clock cycles available between vsync frames. Or it's platform devs that actually know data structures and algorithms, know that a std::list is a linked list, and think they're better suited.

1

u/Sexual_tomato Apr 06 '22 edited Apr 06 '22

Imagine you have a pointer to the first struct of many. It has a pointer to the next struct in the list, and each struct has a pointer to the next one until the pointer is null. Each struct also contains some data. You're on a pointer scavenger hunt where each struct is either the one you want, or the pointer tells you the address where you should look for the next value.

Think of the castles in the original Super Mario Bros; we wanted the princess but when we encountered Toad he told us where the next castle was. We stopped when we found the princess, but we'd have also stopped if we went to all the castles and Peach wasn't at any of them and had been tossed in a lava pit instead.

A doubly linked list is a struct that points to the previous struct in addition to pointing to the next struct. If previous is null, you're at the beginning. If next is null, you're at the end.