how to delete a node c++
void del (node *before_del)
{
node* temp;
temp = before_del->next;
before_del->next = temp->next;
delete temp;
}
how to delete a node c++
void del (node *before_del)
{
node* temp;
temp = before_del->next;
before_del->next = temp->next;
delete temp;
}
how to remove a node from a linked list in c
typedef struct node{
int value; //this is the value the node stores
struct node *next; //this is the node the current node points to. this is how the nodes link
}node;
node *rmvNode(node *head, int index){
node *tmp = head;
node *rmv;
int count = 0;
//base case for if the user enters a value greater then or equal to the length of the head
//base case for if the user enters a value with a list of length 1. because in this library a list MUST contain one value minimum
if(index >= len(head) || len(head) == 1){
return NULL;
}
//if you want to remove the first value
if(index == 0){
rmv = head; //stores the head at this given moment in time
head = tmp->next; //this jumps the position of the head making sure that the beginning is no longer part of the head
free(rmv); //this frees the memory given to the initial head
return head;
}
//if you want to remove index position 1
if(index == 1){
rmv = head->next;
head->next = tmp->next->next;
free(rmv);
return head;
}
//if you want to remove the last value
if(index == -1){
while(count < len(head)-2){ //we do -2 because we want to access the node before the last one
tmp = tmp->next;
count += 1;
}
rmv = tmp->next;
tmp->next = NULL;
free(rmv);
return head;
}
//remove anything else
while(count < index-1){
tmp = tmp->next;
count += 1;
}
rmv = tmp->next;
tmp->next = tmp->next->next;
free(rmv);
return head;
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us