Ravi
Ravi

Reputation: 31437

add node at beginning in c

This is my code to add node at beginning.

void screate(ll *node)
{
  ll *newNode=(ll *)malloc(sizeof(ll));
  printf("Enter number :\t");
  scanf("%d",&newNode->data);
  if(newNode->data != NULL)
  {
    newNode->next=node;
    node= newNode;
    screate(node);
  }
  else
  {
    free(newNode);
    newNode=NULL;
  }
}

Even i found same code here, I unable to figure out, why I getting wrong output.

This is the current node

56->78->77->NULL

But, when i'm trying to add new node at the beginning, then still I'm getting same output i.e. 56->78->77->NULL. Need Help !!

UPDATE

void show(ll *node){
while(node->next != NULL)
 {
  printf("%d->",node->data);
  node=node->next;
 }
printf("NULL");
}

Upvotes: 1

Views: 465

Answers (4)

autistic
autistic

Reputation: 15652

I would suggest changing your screate function so that it returns a struct ll *. The return value would correspond to your new head node.

if(newNode->data != NULL) is not a sensible way to detect input failures from scanf. In fact, that line of code isn't sensible, period. NULL is identified as a member of the set of null pointers. newNode->data is an int, not a pointer. Comparing it to 0 would make more sense. [Here][1] is a standardised scanf reference. Read it carefully and answer the following questions, and you will know how to distinguish between success and failure of scanf:

int x, y;
int z = scanf("%d%d", &x, &y);
int c = getchar();
  1. If I send "abcd{enter}" through stdin, what will z be?
  2. If I send "abcd{enter}" through stdin, what will c be?
  3. If I send an EOF signal through stdin, what will scanf return?
  4. Which value would z be, if it successfully read and put values into the two variables x and y?

Upvotes: 0

Cartroo
Cartroo

Reputation: 4343

You're assigning to node which is just a parameter to the function. Since it's passed by value, this won't update the version held by the calling function.

You need to pass a pointer to it instead (i.e. ll **node) and change your code to assign to *node, and also changer the caller to add a & before the argument to take its address.

void screate(ll **node)
{
  ll *newNode=malloc(sizeof(ll));
  printf("Enter number :\t");
  scanf("%d",&newNode->data);
  if(newNode->data != NULL)
  {
    newNode->next=*node;
    *node= newNode;
    screate(node);
  }
  else
  {
    free(newNode);
  }
}

If you pass a pointer to something to a function, it can change the something but not the pointer itself. The natural conclusion is therefore to make the something itself the pointer - i.e. a pointer to a pointer.

In principle you can take this pointer chaining as deep as you like, but in practice you won't generally need anything more than "pointer to pointer to thing".

Couple of other points. Try to avoid variable names like l and ll as lowercase L's can easily be confused with the digit 1 (and an uppercase I) in many fonts. Also, your use of a recursive call into screate() again would be potentially more efficient as a while loop instead. Some compilers will spot that it's tail-recursive and optimise to a loop anyway, but I never like to rely on that sort of thing when it's just as clear to use a loop in the first place.

Upvotes: 4

sr01853
sr01853

Reputation: 6121

You should change the signature with parameter as a pointer to linked list pointer

void screate(ll **node);

And your corresponding pointer changes in your function must be like this

newNode->next=*node;
*node= newNode;
screate(&node);

Upvotes: 1

cnicutar
cnicutar

Reputation: 182774

newNode->next=node;
node = newNode;

The problem is that node = newNode only changes the local copy of node which means the caller doesn't see the change. For the caller it's as if you never called the function, node still points to wherever it pointed before.

You may want to pass a ll **node and change *node or something else.

Upvotes: 3

Related Questions