Consider a simple representation of the linked list (no dummy nodes). The functions that manipulate these linked lists can be divided into two categories:
1) The function does not change the head pointer: Printing the linked list, there is a function, such as all nodes in the node data members, such as add value update, or node data to access / update other operations .
It is generally easy to determine the prototype of the function in this category. You can loop through / update the list at any time, passing the main pointer as an argument. For example, the following function that adds x to the data members of all nodes.
void addXtoList(struct Node *node, int x) { while(node != NULL) { node->data = node->data + x; node = node->next; } }
2) Function to change head pointer: For example, insert node first (this function always changes head pointer), insert node last (only if first node is inserted) Delete specific node (if the deleted node is the first node, the head pointer changes). There are several ways to update the main pointer with these functions. Let’s explain these methods using the following simple problem.
“If a linked list is provided, write a deleteFirst () function that removes the first node of the given linked list. For example, if the list is 1-> 2-> 3-> 4, 2-> 3 -> should changed to 4.
The algorithm that solves this problem is a simple three-step process. (A) Store the main pointer (b) Change the main pointer to point to the next node (c) Delete the previous main node.
Here are several ways to update the main pointer with deleteFirst () so that the list is updated everywhere.
2.1) Globalize the main pointer: You can make the main pointer global so that the function can access and update it. Below is the C code that uses the global head pointer.
struct Node *head = NULL; void deleteFirst() { if(head != NULL) { struct Node *temp = head; head = head->next; free(temp); } }
This is not the recommended method due to many issues, including:
Because a) the head is globally accessible, it can be changed anywhere in the project, which can lead to unpredictable results. b) If you have multiple linked lists, you need multiple global head pointers with different names.
2.2) Return pointer: You can write deleteFirst () to return a modified pointer. Anyone using this function must update the head node with the return value.
struct Node *deleteFirst(struct Node *head) { if(head != NULL) { struct Node *temp = head; head = head->next; free(temp); } return head; }
There is only one problem with this. Things get confusing if the user doesn’t assign a return value to the header. The C / C ++ compiler allows you to call a function without assigning a return value.
head = deleteFirst(head); deleteFirst(head);
2.3) Using double pointers: This approach will follow some simple C rules. If you want to change a local variable in one function within another, pass a pointer to that variable. Therefore, by passing a pointer to the main pointer, you can change the main pointer in the deleteFirst () function.
void deleteFirst(struct Node **head_ref) { if(*head_ref != NULL) { struct Node *temp = *head_ref; *head_ref = (*head_ref)->next; free(temp); } }
Conclusion:
In this article, you will learn how to write a function in C to change the primary pointer of the linked list. Because fewer can cause problems, this approach is the best of the three.