0

I am practicing a linked-list code. Below is the insert function for that:

Node* insert_at_pos(Node *head, int pos){    
    struct Node *ptr=NULL;
    printf("enter data\n");
    ptr=(Node*) malloc(sizeof(Node));
    scanf("%d",&ptr->data);
    ptr->next=NULL;
    if (pos==0){
        if (head==NULL){
            head=ptr;
            return head; //return that I want to remove
        }
    }
    printf("done\n");
}

Instead of returning Node*, if I return void, I think this code should still work because I am passing value by reference. So the value of head should update automatically instead of returning it, but it doesn't work if I remove the Node* and put void in the return type of insert_at_pos.

And, I am calling insert_at_pos function like this::

Node *head=insert_at_pos(head,0);

What could be the possible explanation or what is going wrong here?

Abhineet
  • 5,320
  • 1
  • 25
  • 43
red5pider
  • 351
  • 1
  • 9
  • 24
  • 2
    Possible duplicate of [How do I modify a pointer that has been passed into a function in C?](http://stackoverflow.com/questions/766893/how-do-i-modify-a-pointer-that-has-been-passed-into-a-function-in-c) – Michael Faisst Oct 20 '16 at 11:43
  • 1
    Yes, it's definitely a duplicate of [How do I modify a pointer that has been passed into a function in C](http://stackoverflow.com/questions/766893/how-do-i-modify-a-pointer-that-has-been-passed-into-a-function-in-c) – Jabberwocky Oct 20 '16 at 12:01

1 Answers1

2

There are basically two ways to handle this problem. Either you pass the address of the pointer (type Node**, pass &head) or you make a separate list type.

The second solution would look something like this:

typedef struct List {
    Node *head;
} List;

A new empty list can then be created like this:

List *list = malloc(sizeof (List));
list->head = NULL;

Both ways are fine. Conceptually, the second solution better matches the actual problem since it differentiates a List from the data Nodes. You can create a list and add or remove values without changing the list handle.

The first solution tries do skip a separate entity for the list by letting the list head be the list handle. The problem with that is that an empty list doesn't have any nodes, so an empty list is denoted by NULL. This means that the list handle changes when the list transitions from empty to non-empty or from non-empty to empty, so when you insert or remove items the list handle might change.

An insert function using the first solution could be declared like this:

void insert(Node **head, int value);

a call would look like this:

Node *head = null;
insert(&head, 42);

Or it could be declared like this (like in your question):

Node* insert(Node *head, int value);

and called like this:

Node *head = null;
head = insert(head, 42);
Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
  • It's working. thanks! but can you please explain why is the code i am using is wrong. conceptually? – red5pider Oct 20 '16 at 13:06
  • 1
    @SamarYadav It's not wrong, it's just another way to do it. I've added an explanation explaining why insert/remove acts weird when the list transitions from empty to non-empty. – Klas Lindbäck Oct 20 '16 at 13:38