I would do it another way... get the input into an int variable, and then only malloc and add the node when it is != 0.
Code:
void take(node **list_end)
{
while(1) {
int a;
node *new_node;
// Get the user input
printf("\n\tEnter the Number(0 to END): ");
if(scanf("%d", &a) != 1)
break; // Input failed
if(a == 0)
break; // User entered a zero, or maybe bad input
// Create a new node
new_node = malloc(sizeof(node));
if(!new_node)
break;
new_node->num = a;
new_node->next = NULL;
// Add it to the list
*list_end = new_node;
list_end = &(new_node->next);
}
}
If you don't mind the list being in reverse order (older at the end) it gives much clearer and cleaner code.
Code:
void take(node **head)
{
while(1) {
int a;
node *new_node;
// Get the user input
printf("\n\tEnter the Number(0 to END): ");
if(scanf("%d", &a) != 1)
break; // Input failed
if(a == 0)
break; // User entered a zero, or maybe bad input
// Create a new node
new_node = malloc(sizeof(node));
if(!new_node)
break;
new_node->num = a;
// Add it to the start of the list
new_node->next = *head; // Next is current head.
*head = new_node; // Head is now the new node.
}
}