Great teacher. I really like how you explain everything more than once in different ways
@Anonomyous-cg1ye2 жыл бұрын
Awesome video and playlist on Linked List. This really helped me understand my homework in a concise but effective manner. Thanks!
@dalibormaksimovic639910 ай бұрын
There is another way to remove a specific node from the list. For instance, if we need to remove elements of the list that are not from some interval [m, n]: void fromInterval(Node** head, int n, int m) { Node* curr = *head; Node* prev = NULL; while(curr != NULL) { if(curr -> data > n || curr -> data < m) { Node* temp = curr; if (prev == NULL) { *head - curr -> next; } else { prev -> next = curr -> next; } curr = curr -> next; free(temp); } else { prev = curr; curr = curr -> next; } } }
@amireid84968 ай бұрын
Hey, I think to remove all the nodes that have the same value, it was enough for me to remove "return;" in the if statement, then it worked out
@Emirodriguezalc3 жыл бұрын
Hi man! I have been watching your videos and would like to buy you a coffee, do you have a link for that? Thank you and keep up the good work!
@CodeVault3 жыл бұрын
code-vault.net/ There's a store on the website! Also we'll implement a support us feature over there soon!
@yoannhoundonougbo36562 жыл бұрын
great video. Can you please do a video on graphs in c? I need it for a pathfinding project of my school. Thanks!
@CodeVault2 жыл бұрын
Yes! It's one of the topics I want to cover in the future. Thanks for the recommendation
@Hello_am_Mr_Jello3 жыл бұрын
is it necessary to put "return;" in every if condition like how you did here?
@CodeVault3 жыл бұрын
Not really, you can also use the else branch but it becomes cluttered (in my opinion)
@DataStructures2 жыл бұрын
what if there is only one node in the linked list though, and you want to remove it?
@CodeVault2 жыл бұрын
That's what the first if in the function is for. It checks if the root is the node you want to remove it and it simply removes it. This works for any number of nodes in the linked list. If the list has only 1 node the *root = (*root)->next; will set the root to NULL which, in our case, means an empty linked list
@mauriciofuentes7638 Жыл бұрын
Hey, i attempted to implement this but ran into segmentation fault errors
@CodeVault Жыл бұрын
Feel free to share the code here or on the Discord server so I can find the issue
@HK-rc6vf3 жыл бұрын
How to delete a tail element and modify the tail?
@CodeVault3 жыл бұрын
You want to delete it or modify it?
@HK-rc6vf3 жыл бұрын
@@CodeVault i want to delete the last element. In the video cases were discussed to delete the HEAD and also to delete other elements beyond HEAD. I tried a few things which didn’t work. Deleting the tail and then updating the previous element next to NULL would make this complete.
@CodeVault3 жыл бұрын
The remove_element function discussed in this video works for any node, the logic is already there. If the node is the root, the root gets replaced, otherwise you just link previous->next to previous->next->next before deleting the node at previous->next
@HK-rc6vf3 жыл бұрын
@@CodeVault I copied the code from the video and tried to delete last element using both codeblocks and also in the tutorialpoint online c compiler. Both work fine except for the deleting the last element. It returns a segmentation fault if we use the code to delete the last element. #include #include typedef struct Node{ int x; struct Node *next; }Node; void insert_first(Node **root, int val){ //all we need to do is to point new element to root and tell that the new element is our new root Node *new_element = malloc(sizeof(Node)); new_element->x = val; new_element->next = *root; *root = new_element; if(*root ==NULL){ *root=new_element; return; } } void remove_element(Node **root, int val){ if(*root==NULL){ return;//as we don't have anything to remove } //special case to remove HEAD if((*root)->x == val){ Node *temp = *root; *root=(*root)->next; free(temp); } //cases other than HEAD Node *current = *root; while(current->next!=NULL){ if(current->next->x==val){ Node *temp = current->next; current->next=current->next->next;//look ahead is the key to maintain the link free(temp); } current=current->next; } } int main() { Node * root = NULL; insert_first(&root,5); // insert_last(&root,54); insert_first(&root,58); insert_first(&root,565); // insert_pos(root,10); // insert_pos(root->next->next,12); // remove_element(&root,565); remove_element(&root,5); //we need to update HEAD as root is the last element in the list Node *current = root; while(current!=NULL){ printf("%d ",current->x); current=current->next; } return 0; }
@CodeVault3 жыл бұрын
Ahhh, I see the issue. There's a check missing at the remove_element call. current = current->next; makes current be NULL, then current->next gives a SIGSEG. The easiest fix is to change the condition: while (current != NULL && current->next != NULL) In the video I added a return; in that if, to prevent this from happening.
@taliakennedy25172 жыл бұрын
thanks king!!!
@synacktra2 жыл бұрын
Thanks again, solved this myself before watching this video. void _remove(Node** root, int vl) { if(*root == NULL) return; Node* aux; if((*root)->ch == vl) { aux = *root; (*root) = (*root)->next; free(aux); return; } Node* current = *root; while(current->ch != vl && current->next != NULL){ aux = current; current = current->next; } if(current->next == NULL) return; aux->next = current->next; free(current); }
@CodeVault2 жыл бұрын
This one is not quite right. I think if you have this case: 1 2 5 And you try to delete 5, it would exit the while loop at the last element and the if (current->next == NULL) would actually make the program return without deleting the element