Javatpoint Logo
Javatpoint Logo

Write C Functions that Modify Head Pointer of a Linked List?

This course will teach us how to change a linked list's head pointer using C functions.

Consider a simple representation of a Linked List (with no dummy nodes). There are two types of functions that work on such Linked lists:

1) Functions that do not Modify the Head Pointer:

Printing a linked list, modifying data members of nodes, such as adding a specific value to all nodes, and other operations that access/update node data are examples of such methods.

In general, determining the prototype of functions in this category is simple. The head pointer can always be used to traverse or update the list. Consider the code below, for instance, which adds x to all node data members.


2) Head Pointer Modification Functions:

Adding a node at the beginning (In this function, the head pointer is still modified), inserting a node at the end (When the first node is inserted, the head pointer is directly affected), and removing a specific node are some examples (The head pointer is changed when the deleted node is the first node). The head pointer can be adjusted in these procedures in a number of different ways. The following straightforward issue will be used to compare various strategies:

" Create a function called deleteFirst() that removes the first node from a given linked list". For instance, if the list is 1->2->3->4, it should be updated to 2->3->4"

The algorithm for solving the problem consists of three simple steps:

(a) Save the head pointer

(b) the head pointer to the following node

(c) Eliminate the preceding head node.

Here are a few techniques for updating the deleteFirst() function's head pointer such that the list is updated everywhere.

2.1) Globalize the Head Pointer: We can globalise the head pointer so that it can be accessed and modified in our method. The following C code makes use of a global head pointer.


This is not a preferred approach because it has numerous issues, including the following:

a) Because the head is globally accessible, it can be changed anywhere in your project, which may result in unexpected outcomes.

b) Many global head pointers with different names are required if there are multiple linked lists.

2.2) Return the modified head pointer: We can modify deletefirst() to return the updated head pointer. This function's caller must utilise the returned value to modify the head node.


This method is far superior to the previous one. There is only one problem with this: if the user forgets to assign the returned value to the head, things become nasty. Calling a function without assigning the returned value is supported by C/C++ compilers.


2.3) Use Double Pointer: This solution adheres to a simple C rule: if we want to edit a local variable of one function within another, send a pointer to that variable. So, in our deletefirst() function, we may send the pointer to the head pointer to edit the head pointer.


This method tends to be the best of the three because there are less issues.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA