There is a lot wrong with it.
- NULL is defined in stdio and stdlib. Redefining it in your program is pointless and stupid.
- main's return type is required to be int. Get a copy of one of the standards. C89's the earliest, C99 has been the major standard for roughly 11 years, and C11 has just recently been published. All of them require you to define main with a return type of int.
- Avoid the use of conio.h, since it's incredibly old and unportable. Your program aims to build a linked list and then print it, you can accomplish that perfectly using only the standard libraries. Using conio.h does more harm than good.
- fflush requires its argument to point to an *OUTPUT* stream. You're passing a pointer to an input stream to it, which results in undefined behaviour.
Your use of scanf is incorrect; it has a return value and you should check it to ensure that scanf successfully read an element of data and that the stream hasn't reached EOF. I suggest you use a combination of fgets and sscanf or strtol instead. Below is an example of how you might implement a linked list, using scanf (in a somewhat limited way) for input.
#include
#include
struct node {
int datum;
struct node *next;
};
static void freelist(struct node *head)
{
while (head) {
struct node *tmp = head->next;
free(head);
head = tmp;
}
}
static struct node *readlist(void)
{
struct node head;
struct node *np = &head;
int num;
while (scanf("%d", &num) == 1) {
np->next = malloc(sizeof *np->next);
if (!np->next) {
freelist(head.next);
fprintf(stderr, "Unable to allocate memory\n");
exit(EXIT_FAILURE);
}
np = np->next;
np->datum = num;
}
np->next = 0;
return head.next;
}
static void printlist(struct node *head)
{
while (head) {
printf("%d -> ", head->datum);
head = head->next;
}
printf("(null)\n");
}
int main(void)
{
struct node *head;
head = readlist();
printlist(head);
freelist(head);
return 0;
}
Given the input: 10 20 30 40<\n>
It will output: 10 -> 20 -> 30 -> 40 -> (null)