Answers for "adding a node in the front on a linked list"

C
0

adding a node in the front on a linked list

/* Given a reference (pointer to pointer) to the head of a list
   and an int,  inserts a new node on the front of the list. */
void push(struct Node** head_ref, int new_data)
{
    /* 1. allocate node */
    struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
  
    /* 2. put in the data  */
    new_node->data  = new_data;
  
    /* 3. Make next of new node as head */
    new_node->next = (*head_ref);
  
    /* 4. move the head to point to the new node */
    (*head_ref)    = new_node;
}
Posted by: Guest on March-27-2022

Code answers related to "adding a node in the front on a linked list"

Code answers related to "C"

Browse Popular Code Answers by Language