edited by
602 views
0 votes
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;
    return;
  }

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

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

  return;
 }
edited by

Please log in or register to answer this question.

Related questions

0 votes
0 votes
1 answer
2
Arnab Bhadra asked Jun 28, 2017
3,377 views
Insertion of a node into a doubly linked list requires how many changes to various Next and Previous Pointer?A. No ChangeB. 1 Next , 1 PreviousC. 2 Next , 2 PreviousD. 3 ...
0 votes
0 votes
2 answers
4