How to write C functions that modify the head pointer of a Linked List?

In the below article we are going to understand how we can modify a function in c. Lets just get into the approach to modify the head pointer in linked list in function of c.

How to Modify Function in C

Explain different ways in which the head pointer of a linked list can be modified inside a C function.

In this article, we are going to look at different ways in which the head of a linked list can be modified. Now, why would we need to modify the head of a linked list?
In a lot of operations, such as insertion and deletion at the beginning, we need to somehow modify the head. Now, the head is of type pointer to node. A pointer passed into a function by value can’t be modified to point to something else, only the data inside it can be changed. So, let’s look at different ways in which we can solve this issue.

Let us take an operation Insertion at the beginning of a linked list, where we need to modify the head of a linked list.

Approach 1 to modify function in c

Declaring head as a global variable.

If we declare head as a global variable, it will be accessible inside every function. So, we will be able to modify it as per our needs.

Code Implementation to modify function in c

Node *head = NULL;
void push_front(int new_val){
    Node* new_node = new Node(new_val);
    new_node->next = head;
    
    head = new_node;
}

Approach 2 on how to modify head pointer in linked list

By returning the head node from the function.

If the head pointer is passed to a function by value, then we can’t change it inside that function. If we return the copy of the head pointer from the function after modifying it and update the original head in the caller function, the head pointer will be modified.

Code Implementation on how to modify head pointer in linked list


Node* push_front(Node* head, int new_val){
    Node* new_node = new Node(new_val);
    new_node->next = head;
    
    head = new_node;
    return head;
}

int main(){
    Node *head = NULL;
    head = push_front(head, 2);
}

The head pointer will be updated in the caller function.

Approach 3 on how to modify head pointer in linked list

By passing the head pointer as a double pointer to the function.

Another way to change the head pointer inside the called function itself is by passing as a double-pointer. In that case, we need to pass the address of the head as an argument to the called function.

Code Implementation on how to modify head pointer in linked list


void push_front(Node** head, int new_val){
    Node* new_node = new Node(new_val);
    new_node->next = (*head);
    
    *head = new_node;
}

int main(){
    Node *head = NULL;
    push_front(&head, 2);
}

All the above approaches can be used depending on the usage to modify the head pointer.

Conclusion

In the above article, we have seen how we can modify function in c. Taking an example of modifying a head pointer in linked list.Concepts like these are very necessary to understand pointers well. I would highly recommend you to practice some problems on the linked list from Linked List.

FAQs

*1. What is the meaning of node &head?*
Node
& head is a reference to a pointer to a node.

*2. What is node& null?*
Whenever we create a linked list, we should initialize it. That’s why we initialize it as null using node
&null.

3. What is a self head?
A self head is the current node so self head is the n.next node where next is the next node and n is self node.

Leave a Reply

Your email address will not be published. Required fields are marked *