0

i have written a code to append nodes if empty or not. I think my code and logic is correct but still i am not able to get any answer . its compiling but after running not showing any result . please tell me why

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

struct node 
{
    int data;
    struct node *nxt;
};

void append(struct node *,int);
void display(struct node*);

void append( struct node *q, int num )
{
    struct node *temp,*r;
    if(q == NULL)
    {
        temp = (struct node*)malloc(sizeof(struct node));
        temp -> data = num;
        temp -> nxt = NULL;
        q = temp;
    }
    else
    {
        temp = q;
        while(temp->nxt != NULL)
        {
            temp = temp->nxt;
        }
        r = (struct node*)malloc(sizeof(struct node));
        r -> data = num;
        r -> nxt = NULL;
        temp->nxt = r;
    }
}

void display(struct node *q)
{
    while(q != NULL)
    {
        printf("%d",q->data);
        q = q->nxt;
    }
}


int main()
{
    struct node *a;
    a= NULL;
    append(a,10);
    append(a,11);
    append(a,12);
    display(a);
    return 0;
}
Sudhanshu Gupta
  • 2,255
  • 3
  • 36
  • 74

2 Answers2

1

You need to pass the address of the first parameter (the list head) to the append method by address. As written, it is passing NULL in the first call (and each subsequent call) because it is passing by value.

The prototype should look something like this:

void append( struct node **q, int num )

And then make calls like this:

append(&a,10);

Note that the function append needs to be updated accordingly to treat the parameter change correctly.

Mark Wilkins
  • 40,729
  • 5
  • 57
  • 110
1

The prototype of append needs to be changed as

void append( struct node **q, int num );

and pass the address of the a as &a to this function. This is because C only supports pass by value. Learn more on this here.

Please find the modified append function as below:

void append( struct node **q, int num ) 
{     
  struct node *temp,*r;     

  if(*q == NULL)     
  {         
     temp = (struct node*)malloc(sizeof(struct node));
     temp -> data = num;
     temp -> nxt = NULL;
     *q = temp;
  }
  else
  {
     temp = *q;
     while(temp->nxt != NULL)
     {
         temp = temp->nxt;
     }
     r = (struct node*)malloc(sizeof(struct node));
     r -> data = num;
     r -> nxt = NULL;
     temp->nxt = r;
 } 
} 

In addition:

Chane the below line:

printf("%d",q->data); 

as

printf("%d\n",q->data); 

printf might not flush data unless there is a newline in some terminals.

Jay
  • 24,173
  • 25
  • 93
  • 141
  • In most systems that I have dealt with, the new line is not necessary to flush the data. However, you are correct that the OP likely does want the newline (or at least a space between the values with a newline at the end of the function). – Mark Wilkins Jun 09 '12 at 16:22
  • @Mark Wilkins, As you say, OP needs a newline for the output to appear clear to the viewer. But, also I have faced this problem long back when I was new to programming, though I don't remember the terminal now. Also, for reference: http://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin – Jay Jun 09 '12 at 16:25
  • The if / else is not needed once you have the double pointer to seek to the end. `void append( struct node **q, int num ) { for(; *q ; q = &(*q)->nxt) {;} *q = malloc(sizeof **q ); (*q)->data = num; (*q)->nxt = NULL; } ` – wildplasser Jun 09 '12 at 16:26
  • @Mark Wilkins, Anyways, I edit my answer to reflect that it might not print in some terminals if newline is not present. I guess using "will not" makes it more of a sure behavior. Thanks for your comment. : ) – Jay Jun 09 '12 at 16:29
  • @wildplasser, Got it. : ) I usually write it in the same manner as OP, as I find such kind of code more readable. – Jay Jun 09 '12 at 16:40
  • Thanks everyone for the answers , i got where i was wrong , thank you:) – Sudhanshu Gupta Jun 09 '12 at 17:36