In the code snippet below the pre-increment operator used in the main
function results in values starting from 2 while the post increment values start from 1 when inserting to the list. I am unable to figure out why.
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
void insert_node(int new_data, Node **head_ref) {
Node *node= (Node *)malloc(sizeof (Node));
node->data = new_data;
node->next = *head_ref;
*head_ref = node;
}
void display(Node *head) {
Node *traverse = head;
while (traverse->next != NULL) {
printf("\ndata=%d", traverse->data);
traverse = traverse->next;
}
}
void main() {
Node *pdata;
Node *list_head = NULL;
int i = 0;
while (i <= 10)
insert_node(++i, &list_head);
display(list_head);
}