Deallocating a linked list

  Рет қаралды 15,743

CodeVault

CodeVault

Күн бұрын

Check out our Discord server: / discord

Пікірлер: 36
@suryar1770
@suryar1770 5 ай бұрын
the best tutorial about linked list in youtube. Thank you for sharing your knowledge..
@raresdammon
@raresdammon Жыл бұрын
Best tutorial on linked lists I've found so far on KZbin! Thanks mate!
@japhethjay4880
@japhethjay4880 Жыл бұрын
Your videos are amazing and am really recommending them, keep up the great work, what will be even more amazing will be using a white board to draw out certain concepts, like am still having difficulties understanding why we pass a double pointer, and how just dereferencing it once gets us the last node.
@CodeVault
@CodeVault Жыл бұрын
I'll keep that in mind for future videos. Thanks for the feedback. Regarding the double pointer. The answer is simple, when deallocating a list, we have to set the root pointer to be NULL. We have something like this in the main function: Node* root; But, just passing this to a function and setting it to NULL is only going to change that parameter: void test(Node* n) { n = NULL; // this only sets the variable n to be NULL. n is a copy of the root variable from main, therefore it doesn't change anything in main } int main() { Node* root; // assume linked list is populated test(root); // root will NOT be NULL here } Same thing but with double pointers would work: void test(Node** n) { *n = NULL; // this sets to NULL whatever n is pointing to. n here is pointing to root in this case } int main() { Node* root; // assume linked list is populated test(&root); // we get the address of root so that n inside the test function points to root // root will be NULL here } Hope this gives a clear idea of why we need double pointers
@japhethjay4880
@japhethjay4880 Жыл бұрын
@@CodeVault well in a way I'll over it a couple of times to digest it
@edwarddemesaify
@edwarddemesaify 2 жыл бұрын
Your tutorials are awesome and I really appreciate you. I do have a request. Would it be possible for you to draw pictures, especially the nodes and the arrows and the double pointers, etc… to represent the logic of linked list algorithm (as your explain the concept)? I know personally it would really help me out in terms of being able to understand it fully and fill in my comprehension gap. And once again, thank you!!!!
@CodeVault
@CodeVault 2 жыл бұрын
Thanks! I'll keep this in mind.
@turuus5215
@turuus5215 2 жыл бұрын
@@CodeVault Yes, please. I went on to watch your other videos as well as some other tutor’s videos to grasp double pointers, arrows etc. which my understanding lacked at. We appreciate your help.
@onaecO
@onaecO Жыл бұрын
You are the best, thx for wha you're doing!
@hereusername
@hereusername 3 жыл бұрын
Very good explanation
@tiburaishimura3774
@tiburaishimura3774 2 жыл бұрын
Thanks for very good explanation Sir! Is it possible to dealocate the linked list with a recursion ?
@Amolang991
@Amolang991 3 жыл бұрын
thx for the lecture
@noeelreeds
@noeelreeds 8 ай бұрын
Thank you
@evolagenda
@evolagenda 2 жыл бұрын
With putting Node* aux = curr; in a loop is there any reason you wouldn't do that? I mean would you put the assignment outside of the loop and just usr aux = curr;?
@CodeVault
@CodeVault 2 жыл бұрын
You can do that as well. I added the definition of the variable in that block simply because it's only used there and I don't need it outside the block
@user-ir8nd6mj2b
@user-ir8nd6mj2b Жыл бұрын
11:11 my solution (using head recursion): void deallocate(Node **node) { if(*node == NULL) return; deallocate(&(*node)->next); free(*node); *node = NULL; }
@CodeVault
@CodeVault Жыл бұрын
Looks good. Good job!
@amireid8496
@amireid8496 8 ай бұрын
we are using aux pointer because it is going to be on the stack memory not on the heap. right?
@CodeVault
@CodeVault 7 ай бұрын
We are using it to be able to both free it and assign it a different value. Has nothing to do with stack/heap memory
@gottod6895
@gottod6895 8 ай бұрын
Your contributions are absolutely helpful, I made the mistake of trying free(aux) before changing curr's value, that gave me a segmentation fault for obvious reasons, I danced around the issue by further complicating things, by creating a dynamic array of node pointers (Node **) where the nodes to be freed are stored, and using two loops one for filling the array and another to do the deallocating of nodes one by one. And to be able to do that I needed to know the size of the LinkedList from another function alltogether, which again adds to the complexity, and in the end deallocating the array than deallocating the linked list iteself, I cannot believe how stupid of me to do that.
@synacktra
@synacktra 2 жыл бұрын
recursive deallocation function: void recursive_dalloc(Node** root) { Node *current_node = *root; Node* previous_node = current_node; current_node = current_node->next; free(previous_node); if(current_node != NULL) recursive_dalloc(&current_node); *root = NULL; }
@CodeVault
@CodeVault 2 жыл бұрын
Looks good. Good job!
@michalbotor
@michalbotor Жыл бұрын
thank you very much! i was stuck on this one. i will only add my code to let other noobies like me have a side by side comparison with a slightly different notation: ``` void linkedlist_free(Node** p_root) { Node* curr = *p_root; while (curr != NULL) { Node* aux = curr; curr = curr->next; free(aux); } *p_root = NULL; } void linkedlist_freerec(Node** p_root) { Node* curr = *p_root; Node* aux = curr; curr = curr->next; free(aux); if (curr != NULL) { linkedlist_freerec(&curr); } *p_root = NULL; } ``` basically the code is the same. the while block is moved outside. and the checking condition is moved below. that's where the recursive call happens.
@michalbotor
@michalbotor Жыл бұрын
what i wrote was actually incorrect, here's a corrected version: ``` void linkedlist_free(Node** p_root) { Node* curr = *p_root; while (curr != NULL) { Node* aux = curr; curr = curr->next; free(aux); } *p_root = NULL; } void linkedlist_freerec(Node** p_root) { Node* curr = *p_root; if (curr != NULL) { Node* aux = curr; curr = curr->next; free(aux); linkedlist_freerec(&curr); } *p_root = NULL; } ``` previous one was failing when *p_root = NULL.
@iradufashaBikri
@iradufashaBikri 7 ай бұрын
@HomelessDeamon
@HomelessDeamon Жыл бұрын
what if you have a loop in linked list?
@CodeVault
@CodeVault Жыл бұрын
You can use the function implemented in this video before iterating a linked list: code-vault.net/lesson/2t4nd0hjh5:1603732280242
@sebastiano061
@sebastiano061 4 ай бұрын
I have a question, don't know if you're gonna reply because the video is 4 years old, but I hope so: when you define the deallocate function, why do you use a double pointer to Node type in the formal parameters of the function? Shouldn't just one be enough? And then call it in the main: deallocate(root);
@CodeVault
@CodeVault 3 ай бұрын
Because of the last line that says: *root = NULL; This sets the root value (in the main function) to be NULL. Since root is a single pointer in main... to change it, we need a double pointer Sorry for the late response
@sebastiano061
@sebastiano061 3 ай бұрын
@@CodeVault thanks for your response 🙏🏻 better late than never. So you're passing a pointer by reference in the main function if I understood, right?
@CodeVault
@CodeVault 3 ай бұрын
@@sebastiano061 Yeah. That's basically it
@sickdewd5694
@sickdewd5694 2 жыл бұрын
what do you do with gcc
@CodeVault
@CodeVault 2 жыл бұрын
gcc is a compiler to make executable code from C source code.
@sickdewd5694
@sickdewd5694 2 жыл бұрын
@@CodeVault Yeah ik that but you said that there is a different step you take when using gc and I was wondering what that was.
Short introduction to doubly linked lists
9:59
CodeVault
Рет қаралды 5 М.
Why You Should AVOID Linked Lists
14:12
ThePrimeTime
Рет қаралды 271 М.
Дарю Самокат Скейтеру !
00:42
Vlad Samokatchik
Рет қаралды 2,8 МЛН
I CAN’T BELIEVE I LOST 😱
00:46
Topper Guild
Рет қаралды 108 МЛН
Looks realistic #tiktok
00:22
Анастасия Тарасова
Рет қаралды 96 МЛН
Sorted linked list
18:11
CodeVault
Рет қаралды 16 М.
Removing an element from a linked list
13:15
CodeVault
Рет қаралды 14 М.
Arrays as function parameters in C
13:28
CodeVault
Рет қаралды 12 М.
Singly Linked Lists Tutorial - What is a Linked List?
13:31
Tech With Tim
Рет қаралды 41 М.
Learn Linked Lists in 13 minutes 🔗
13:24
Bro Code
Рет қаралды 266 М.
Good practice for freeing memory in C
8:17
CodeVault
Рет қаралды 12 М.
Iterating over a linked list in C
11:45
CodeVault
Рет қаралды 35 М.
Дарю Самокат Скейтеру !
00:42
Vlad Samokatchik
Рет қаралды 2,8 МЛН