Quote:
else {
while (temp!='\0'){
temp=temp->next;
}
temp->data=data;
temp->next='\0';
}
Should be, instead
else
{
while ( temp->next )
{
temp = temp->next;
}
temp->next = (struct Node *)malloc(sizeof(struct Node));
temp->next->data = data;
temp->next->next = NULL;
}
Please note, in
C++
code, use:
nullptr
(instead of '\0'
or NULL.
)new
instead of malloc
.Node
constructor to initialize it.
At least, if you insist using
C
-like programming, always check the return value of
malloc
.