I am going through some tutorials, this one in particular is for linked lists and I am facing a roadblock here, I got:
#include<iostream>
#include<stdlib.h>
using namespace std;
struct Node{
int data;
Node *next;
};
Node *tail, *head;
class singly{
public:
singly(){
head = NULL;
tail = NULL;
}
void deletenode(struct Node **head_ref, int key){
struct Node *temp = *head_ref, *prev;
if(temp != NULL and temp -> data == key){
*head_ref = temp -> next;
free(temp);
return;
}
while (temp != NULL && temp -> data != key){
prev = temp;
temp = temp -> next;
}
if(temp == NULL) return;
prev -> next = temp -> next;
free(temp);
}
};
int main{}
my issue in understanding this piece of code is the deletenode function, in particular with the way struct is used, first, what is the meaning of defining struct Node **head_ref
?, second, what in itself is happening with?:
struct Node *temp = *head_ref, *prev;
as such I am not sure what is the relationship between *head_ref, *prev, *temp, and further down the code
*head_ref = temp -> next;
and
prev = temp;
make little sense to me because of this. What I am missing?
instead of writing your own code like that. That code is more indicative of "learning datastructures using C++" (with datastructures requiring a lower level kind of coding then higher level C++ which is used more nowadays)
– Pepijn Kramer Oct 03 '22 at 04:49