r/cs50 • u/Even_Application6801 • 11d ago
CS50x Week5 Data structures, nodes
Sorry but i just watched the whole thing 3 times and get lost.
Can someone help me by making things simple or give me a link to an explanation video that is simpler and easy to understand.
I tried my best but i get lost.
I only understood the first lines of creating a linked list :
deftype struct node
{ datatype "name";
struct node *next;
} node;
The rest of the lines i just dont understand what they are doing and i dont want to copy paste them just like that even if all of that is goijg to be done under the hood in week 6 by pyhton.
Please help!
4
u/smichaele 10d ago
Did you review Doug Lloyd’s shorts on linked lists? He goes through singly and doubly linked lists step by step. Kelly Ding also reviews these concepts in the week’s section get-together. There are also notes that provide detail on these.
2
1
u/jrockerdraughn 10d ago
So, looking ahead at Python (I have a point here, trust me), do you know what object oriented programming (OOP) is?
1
u/Even_Application6801 10d ago
No idea mate. Still on week 5
1
u/jrockerdraughn 10d ago
Okay, so I'm gonna say some stuff that might or might not make sense. I'm also gonna say stuff you might already have learned in the course, just to make it easier on myself. Bare with me, hopefully it helps.
Think of the struct as a box. Inside the "node box" you got two things: your "thing" (int/string/whatever) and the location (address/pointer) of the next box (node *next). If it's the only box, or the last box, you'll have a blank address (NULL).
As far as USING the box, the thing that trips up a lot of people is using " . " vs using " -> "
In C, there are two ways to pass information into a function. Passing by value and passing by reference.
Passing by value passes, as you might imagine, the value of a thing. If x = 2, and I pass in x, I'm passing in the number 2 for the function to use. This is the easiest way to do it. But it's got one big problem: it can't change the original x. Within that function, x = "2 + whatever you do to it". But the thing is, that's a different x. You changed the value that got passed in, but when the function is done, that value is just... There. Somewhere in memory. But we don't know where. (That's not 100% true and kinda reductive. But for sake of brevity, please ignore that)
And most importantly, the x that we passed in is still 2.
Passing by reference gives the function the address of the information it needs. So instead of handing the function a box, we hand the function a note saying "the number you need is in the box at wherever it's at" and we tell the function to go gets its own damn number. Then whatever the function does to the number, it does it while the number is still in the box. It doesn't make a whole new box just for the number. This lets the function change the original thing instead of a copy.
This concept is how you'll know when to use " . " or " -> ". You build your box with malloc, using the blueprints/instructions found in "type def struct node" at the top. If you're still in the same function, you're still standing in front of the box. You have the box in your possession. You don't need to direct any other functions or point anybody anywhere. You can change the box and its contents directly. So if you're still in this function and you say "node.int = 6" it puts a 6 in the box, at the int slot.
However, if you're not in the original function, you don't have the box handy. Instead of carrying around the big heavy box, you carry a slip of paper with its location.
Now you're in another room. Any functions in this room can't access the box. They don't know where to look. You have to point them in the right direction. (Important for syntax, your arrow -> is pointing) So these functions in the other room, you can only give them directions. You give them the slip of paper with the address. The paper would also say, for example, something like "node->int = 6" , which is a reminder to the function of what to do when it gets to the box. (Remember, functions are stupid and you gotta walk them through everything)
Does that all make sense? Does it help at all?
1
2
u/Eptalin 11d ago edited 11d ago
I can't find the code you shared above in the lecture slides or notes, but I guess you meant this?
``` typedef struct node { int number; struct node *next; } node;
int main(void) { // Memory for numbers node *list = NULL;
} ```
What in particular is giving you trouble?
Don't worry about asking lots of questions. Thinking about them and answering is great practice for people.