175 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;
return;
}

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

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

return;
}
in DS
edited
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$

## Related questions

1
1.1k views
Consider the following function that takes reference to head of a Doubly Linked List as parameter. Assume that a node of doubly linked list has previous pointer as prev and next pointer as next. void fun(struct node **head_ref) { struct node *temp = NULL; struct node *current = *head_ref; while (current != NULL) { temp = current- ... 4 <--> 3 <--> 2 <--> 1. D) 6 <--> 5 <--> 4 <--> 3 <--> 1 <--> 2
In general merge sort is not considered in-place sorting technique. Because an auxiliary array is used. If we will try to do it in-place in array data structure then our merge procedure will take O($n^2$ ... using a doubly linked list in place of Array (for storing and merging data) ? Please share your valuable opinion. It will be great help.