The Gateway to Computer Science Excellence
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 by (117 points)
edited by | 77 views
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
2 answers
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,737 questions
57,271 answers
104,780 users