-1

I m trying to insert a newNode at the beginning of a linked list in C language. I have tried other methods such as returing the newNode from the insertAtBeginnning function and then setting the head as that newNode in the main function & that works fine. But what I want to do is use a void function for the case and change the head there itself which is not working. Here's the complete code :

#include <stdio.h>
#include <stdlib.h>

struct Node
{
    int data;
    struct Node *next;
};

void linkedListTraversal(struct Node *ptr)
{
    while (ptr != NULL)
    {
        printf("element: %d\n", ptr->data);
        ptr = ptr->next;
    }
}

void insertAtBeginning(struct Node *head, int data)
{
    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));

    newNode->data = data;
    newNode->next = head;

    head = newNode;
}

void insertAtIndex(struct Node *head, int data, int index)
{
    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
    struct Node *temp = (struct Node *)malloc(sizeof(struct Node));
    temp = head;
    int i = 0;
    while (i < index - 1)
    {
        temp = temp->next;
        i++;
    }
    newNode->data = data;
    newNode->next = temp->next;
    temp->next = newNode;
}

void insertAtEnd(struct Node *head, int data)
{
    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
    struct Node *temp = (struct Node *)malloc(sizeof(struct Node));
    temp = head;
    while (temp->next != NULL)
    {
        temp = temp->next;
    }
    temp->next = newNode;
    newNode->data = data;
    newNode->next = NULL;
}

void insertAfterNode(struct Node *previous, int data)
{
    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
    struct Node *temp = (struct Node *)malloc(sizeof(struct Node));

    newNode->data = data;

    newNode->next = previous->next;
    previous->next = newNode;
}

int main()
{
    struct Node *head;
    struct Node *second;
    struct Node *third;
    struct Node *fourth;

    // Allocate memory for the nodes of the linkedLists in the heap
    head = (struct Node *)malloc(sizeof(struct Node));
    second = (struct Node *)malloc(sizeof(struct Node));
    third = (struct Node *)malloc(sizeof(struct Node));
    fourth = (struct Node *)malloc(sizeof(struct Node));

    // Link first and second nodes
    head->data = 12;
    head->next = second;

    // Link second and third nodes
    second->data = 98;
    second->next = third;

    // Link third and fourth nodes
    third->data = 38;
    third->next = fourth;

    // Terminate the linkedList at the fourth node
    fourth->data = 37;
    fourth->next = NULL;

    printf("Linked list before insertion\n");
    linkedListTraversal(head);

    insertAtBeginning(head, 20);

    // insertAtIndex(head, 22, 3);

    // insertAtEnd(head, 110);

    // insertAfterNode(second, 27);

    printf("Linked list after insertion\n");
    linkedListTraversal(head);

    return 0;
}

Here i have also used other cases of insertion in linked list and all are working fine instead of just insertAtBeginning function :( Any help will be greatfull.

  • 1
    Does this answer your question? [How do I modify a pointer that has been passed into a function in C?](https://stackoverflow.com/questions/766893/how-do-i-modify-a-pointer-that-has-been-passed-into-a-function-in-c) – UnholySheep Oct 12 '22 at 13:39
  • Kind of but didn't get the point that in other cases everything is working fine after passing a pointer in the function , then why in this case i want to pass pointer to a pointer and how shall i actually do it? – Neilson Programmer Oct 12 '22 at 13:42
  • You can only modify a pointer in a function if you pass the address of that pointer (i.e. a pointer to a pointer) to your function. In the other cases the pointer is not change but only the memory where it points to (either directly or after following `next` pointers a few times). – Gerhardh Oct 12 '22 at 14:30

1 Answers1

1

head is the entry point for your list. Inserting an element at the beginning should make head point to this new element. To do it from a void function, you have to pass the address of head so that head is modified by the function:

void insertAtBeginning(struct Node **head, int data)
{
    struct Node *newNode = malloc(sizeof(struct Node));

    newNode->data = data;
    newNode->next = *head;

    *head = newNode;
}

The call to the function will therefore be:

insertAtBeginning(&head, 20);

Another solution would be to have the function return the new address of head.

PS: we don't cast the return from malloc in C and we should check that it succeeded.

CGi03
  • 452
  • 1
  • 3
  • 8