Removing an element from a linked list

  Рет қаралды 14,039

CodeVault

CodeVault

4 жыл бұрын

Check out our Discord server: / discord

Пікірлер: 26
@tungamarkal9552
@tungamarkal9552 Жыл бұрын
Great teacher. I really like how you explain everything more than once in different ways
@Anonomyous-cg1ye
@Anonomyous-cg1ye Жыл бұрын
Awesome video and playlist on Linked List. This really helped me understand my homework in a concise but effective manner. Thanks!
@yoannhoundonougbo3656
@yoannhoundonougbo3656 2 жыл бұрын
great video. Can you please do a video on graphs in c? I need it for a pathfinding project of my school. Thanks!
@CodeVault
@CodeVault 2 жыл бұрын
Yes! It's one of the topics I want to cover in the future. Thanks for the recommendation
@taliakennedy2517
@taliakennedy2517 2 жыл бұрын
thanks king!!!
@Emirodriguezalc
@Emirodriguezalc 3 жыл бұрын
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!
@CodeVault
@CodeVault 2 жыл бұрын
code-vault.net/ There's a store on the website! Also we'll implement a support us feature over there soon!
@amireid8496
@amireid8496 4 ай бұрын
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
@Hello_am_Mr_Jello
@Hello_am_Mr_Jello 3 жыл бұрын
is it necessary to put "return;" in every if condition like how you did here?
@CodeVault
@CodeVault 3 жыл бұрын
Not really, you can also use the else branch but it becomes cluttered (in my opinion)
@dalibormaksimovic6399
@dalibormaksimovic6399 7 ай бұрын
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; } } }
@DataStructures
@DataStructures Жыл бұрын
what if there is only one node in the linked list though, and you want to remove it?
@CodeVault
@CodeVault Жыл бұрын
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
@synacktra
@synacktra 2 жыл бұрын
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); }
@CodeVault
@CodeVault 2 жыл бұрын
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
@mauriciofuentes7638
@mauriciofuentes7638 Жыл бұрын
Hey, i attempted to implement this but ran into segmentation fault errors
@CodeVault
@CodeVault Жыл бұрын
Feel free to share the code here or on the Discord server so I can find the issue
@HK-rc6vf
@HK-rc6vf 3 жыл бұрын
How to delete a tail element and modify the tail?
@CodeVault
@CodeVault 3 жыл бұрын
You want to delete it or modify it?
@HK-rc6vf
@HK-rc6vf 3 жыл бұрын
@@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.
@CodeVault
@CodeVault 3 жыл бұрын
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-rc6vf
@HK-rc6vf 3 жыл бұрын
@@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; }
@CodeVault
@CodeVault 3 жыл бұрын
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.
Reversing a linked list
9:42
CodeVault
Рет қаралды 7 М.
КАРМАНЧИК 2 СЕЗОН 7 СЕРИЯ ФИНАЛ
21:37
Inter Production
Рет қаралды 534 М.
Heartwarming: Stranger Saves Puppy from Hot Car #shorts
00:22
Fabiosa Best Lifehacks
Рет қаралды 21 МЛН
Wait for the last one! 👀
00:28
Josh Horton
Рет қаралды 161 МЛН
Remove Linked List Elements - Leetcode 203
7:51
NeetCode
Рет қаралды 57 М.
Sorted linked list
18:11
CodeVault
Рет қаралды 16 М.
Dependency Injection, The Best Pattern
13:16
CodeAesthetic
Рет қаралды 763 М.
Single Linked List (Deleting the Node at a Particular Position)
9:43
The size of your variables matters.
11:03
Core Dumped
Рет қаралды 102 М.
Understanding and implementing a Linked List in C and Java
18:15
Jacob Sorber
Рет қаралды 233 М.
How to properly deal with dynamically allocated memory
13:44
CodeVault
Рет қаралды 9 М.
КАРМАНЧИК 2 СЕЗОН 7 СЕРИЯ ФИНАЛ
21:37
Inter Production
Рет қаралды 534 М.