Log In
0 votes

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;

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

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

in DS
edited by
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$

Please log in or register to answer this question.

Related questions

0 votes
1 answer
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
asked Aug 28, 2017 in DS mayurp7 1.1k views
0 votes
1 answer
Insertion of a node into a doubly linked list requires how many changes to various Next and Previous Pointer? A. No Change B. 1 Next , 1 Previous C. 2 Next , 2 Previous D. 3 Next , 3 Previous
asked Jun 28, 2017 in DS Arnab Bhadra 345 views
5 votes
1 answer
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.
asked Nov 2, 2017 in Algorithms Chhotu 594 views
0 votes
2 answers