Removing an element from a linked list

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

CodeVault

CodeVault

Күн бұрын

Пікірлер: 26
@tungamarkal9552
@tungamarkal9552 Жыл бұрын
Great teacher. I really like how you explain everything more than once in different ways
@Anonomyous-cg1ye
@Anonomyous-cg1ye 2 жыл бұрын
Awesome video and playlist on Linked List. This really helped me understand my homework in a concise but effective manner. Thanks!
@dalibormaksimovic6399
@dalibormaksimovic6399 10 ай бұрын
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; } } }
@amireid8496
@amireid8496 8 ай бұрын
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
@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 3 жыл бұрын
code-vault.net/ There's a store on the website! Also we'll implement a support us feature over there soon!
@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
@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)
@DataStructures
@DataStructures 2 жыл бұрын
what if there is only one node in the linked list though, and you want to remove it?
@CodeVault
@CodeVault 2 жыл бұрын
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
@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.
@taliakennedy2517
@taliakennedy2517 2 жыл бұрын
thanks king!!!
@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
Reversing a linked list
9:42
CodeVault
Рет қаралды 7 М.
Fake watermelon by Secret Vlog
00:16
Secret Vlog
Рет қаралды 26 МЛН
pumpkins #shorts
00:39
Mr DegrEE
Рет қаралды 124 МЛН
Add to the beginning of a doubly linked list
12:23
CodeVault
Рет қаралды 3,6 М.
All you need to know about linked lists, Libft
38:04
Oceano
Рет қаралды 6 М.
Back To Basics: C++ Containers
31:41
javidx9
Рет қаралды 186 М.
Remove Nodes From Linked List - Leetcode 2487 - Python
13:44
NeetCodeIO
Рет қаралды 11 М.
Removing a node from doubly linked lists
11:18
CodeVault
Рет қаралды 7 М.
Read an array of structs in C
12:37
CodeVault
Рет қаралды 32 М.
Dear Game Developers, Stop Messing This Up!
22:19
Jonas Tyroller
Рет қаралды 721 М.
Deallocating a doubly linked list
9:32
CodeVault
Рет қаралды 3,1 М.
Finding loops/cycles in a linked list
9:26
CodeVault
Рет қаралды 4,5 М.
Fake watermelon by Secret Vlog
00:16
Secret Vlog
Рет қаралды 26 МЛН