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

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.

Recommended Articles:

  1. Traversal in Circular Linked List
  2. How to split a circular linked list into two halves?
  3. C++ program to insert in a sorted circular linked list?
  4. Insertion in Doubly Linked List
  5. Deletion in Doubly Linked List
  6. How to reverse a doubly linked list?
  7. QuickSort on Doubly Linked List
  8. Merge Sort for Doubly Linked List
  9. How to reverse a string using stack?
  10. Insertion in Circular Singly Linked List
  11. How to reverse a linked list?
  12. How to swap nodes in a linked list without Swapping data?
  13. Find Length of a Linked List (Iterative and Recursive)?
  14. A Programmer’s approach of looking at Array vs. Linked List
  15. Deletion in Linked List
  16. How to delete a node in a linked list at a given Location?
  17. Insertion in Linked List
  18. Differences between Arrays and Linked Lists?
  19. Introduction to Linked List

References:

Help Me Please: If you found any error in the content or example please let us know by comment also write your suggestion or if any doubt in the concept please feel free to write us in the comment section.

Practice your Code online here

(Visited 58 times, 1 visits today)

0 0 vote
Article Rating
Subscribe
Notify of
4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
trackback

[…] How to write C functions that modify head pointer of a Linked List? […]

trackback

[…] How to write C functions that modify head pointer of a Linked List? […]

trackback

[…] How to write C functions that modify head pointer of a Linked List? […]

trackback

[…] How to write C functions that modify head pointer of a Linked List? […]

You cannot copy content of this page
Scroll Up
4
0
Would love your thoughts, please comment.x
()
x

  • Name

  • Minimum length of 8 characters.