0 votes
67 views

why we use double pointer struct Node** head here? can anyone explain with details

/* Given a reference (pointer to pointer) to the head of a DLL and an int, appends a new node at the end */

void append(struct Node** head_ref, int new_data)
{
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));

struct Node* last = *head_ref; /* used in while loop below*/

new_node->data = new_data;
/*This new node is going to be the last node, so make next of it as NULL*/
new_node->next = NULL;

/*node as head */
if (*head_ref == NULL)
{
new_node->prev = NULL;
*head_ref = new_node;
return;
}

while (last->next != NULL)
last = last->next;

last->next = new_node;
new_node->prev = last;

return;
}
in DS
edited | 67 views
+1
if suppose $H$ is a node pointer pointing to the $1^{st}$ node of the DLL and suppose address of $1^{st}$ node is $1000$.

So $H$ stores the address $1000$ and let $H$ is stored at the address $9500$ in the memory

Then $^{**}head$ is a pointer pointing to a node pointer. So it can point to $H$

if it points to $H$ then $head$ stores the address $9500$.

$^{*}head$ means we are dereferencing $head$ one time

= $^{*}$(value stored at $head$)

= $^{*} (9500)$ (value stored at 9500)

= $1000$

This means to access the address of  $1^{st}$ node either we can write $H$ or we can write $^{*}head$

In your code they have used $^{*}head$ instead of using $H$

0 votes
1 answer
1
0 votes
1 answer
2
0 votes
2 answers
4