1

I am studying how to insert node at the beginning and I came through this code which I am unable to understand.
I am not getting the print function .

typedef struct node
{
    int data;
    struct node *next;
} Node;
Node *head;
void insert(int x)
{
    Node *temp=(Node*)malloc(sizeof(Node));   
    temp->data=x;
    temp->next=head;
    head=temp;
}
void print()
{
    Node *temp=head;
    printf("List is:");
    while(temp!=NULL) //Unable to understand this statement as how will the loop terminate?                  
    {
        printf("%d ",temp->data);
        temp=temp->next;
    }
    printf("\n");
}
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
Beginner
  • 721
  • 11
  • 27
  • 1
    the line `temp=temp->next;` changes temp, at the end of list next points to NULL, so the condition `temp!=NULL` is no longer true – sp2danny Feb 10 '15 at 13:00
  • Please indent your code correctly. – Tim Čas Feb 10 '15 at 13:00
  • 1
    Run in a debugger, step through the code line by line, see what happens. – Some programmer dude Feb 10 '15 at 13:01
  • So " next" is automatically initialized to NULL at the end as I haven't initialized anywhere it to be NULL. – Beginner Feb 10 '15 at 13:02
  • no automagic. the code you found assumes the list is NULL terminated – sp2danny Feb 10 '15 at 13:04
  • @Beginner: See my answer. Because `head` is has "static storage duration" it is initialized to `NULL` at program start up. As I point out, that isn't something to rely on. Not because it's unreliable but because it reduces readability. Check out the top answer here: http://stackoverflow.com/questions/60653/is-global-memory-initialized-in-c NB: "global memory" and "global variable" should only be used informally when talking about `C`. – Persixty Feb 10 '15 at 13:16

3 Answers3

2

Assume your linked list looks like

a->b->c->d
|
|
head

So now we use a temporary variable

temp = head;

and

while(temp != NULL)
{
  //Keep moving temp
  printf("%d\n",temp->x);
  temp = temp->next;
}

So head is never moved and it is just the temporary pointer which is moved till end of the list the end of the list is got when we reach temp = NULL

a->b->c->d
|
|
temp = head

temp = temp->next;

    a->b->c->d
    |  |
    |  |
  head temp

Repeating the above move until temp = NULL which is TRUE when the last node contents are printed and we do temp = temp->next;

Gopi
  • 19,784
  • 4
  • 24
  • 36
  • But where I have initialised it be Null? – Beginner Feb 10 '15 at 13:08
  • @Beginner You are not initializing it to NULL. You initially make `temp=head` and when the last node is accessed what you have is `temp->next = NULL` so we assing `temp = temp->next (= NULL)` – Gopi Feb 10 '15 at 13:10
  • 1
    Okay Now I am getting it after traversing it ,at the end the next pointer doesn't refers to any element so it is NULL. – Beginner Feb 10 '15 at 13:19
0

A C-Program need a main function to start with. If you want to test the code above, you need to add a main function to initialize head to NULL at start, call insert() several times and print() to check the results.

user3767013
  • 236
  • 1
  • 4
0

Indentation on the Question is a mess.

But the print function is:

  void print() {
      Node *temp=head;             
      printf("List is:");         
      while(temp!=NULL) {         
          printf("%d ",temp->data);
          temp=temp->next;
      }
   }

If head==NULL then temp==NULL at the start and the loop will terminate immediately.

If head!=NULL then temp!=NULL at the start and the data for the head element will be output.

The line temp=temp->next; will then make temp point to the next Node in the list and the loop will advance. If there's only on Node in the list the loop will then terminate.

Otherwise it will print the data for that next element and the line temp=temp->next will move to the third Node (if any).

And so on...

NB: The line Node *head; is not recommended. It will be initialized to NULL because it has static storage duration. But Node *head=NULL; is recommended for future-proofing and readability.

Persixty
  • 8,165
  • 2
  • 13
  • 35
  • @Beginner. For example, you've raised the issue "But where I have initialised it be Null?". Wouldn't it be clearer where the `NULL` was coming from if that line read `Node *head=NULL;'? I think so. The rule about initialization in 'C' isn't simple and almost all other data isn't initialized. That makes it easy to make mistakes or make changes that break code. If you moved 'Node *head;' into 'main()' (recommended) the assurance of initialization wouldn't apply and your program would mysteriously seg-fault. – Persixty Feb 10 '15 at 14:03