I have two functions that should be doing the same thing, and I can't quite figure out why the first works but the second doesn't.
1)
Code:
NodeT *addNode(NodeT *list) {
NodeT *new = NULL;
NodeT *cur = NULL;
new = malloc(sizeof(NodeT));
if (new == NULL) {
fprintf(stderr, "Out of memory\n");
exit(1);
}
if (list == NULL) {
list = new;
} else {
cur = list;
while (cur->next != NULL) {
cur=cur->next;
}
cur->next = new;
}
new->next = NULL;
return list;
}
2)
Code:
NodeT *addNode(NodeT *head) {
NodeT *cur = NULL;
NodeT *new = NULL;
new = malloc(sizeof(NodeT));
if (new == NULL) {
fprintf(stderr, "Out of memory\n");
exit(1);
}
if (head == NULL) {
head = new;
} else {
cur = head;
while (cur != NULL) {
cur = cur->next;
}
cur = new;
}
new->next = NULL;
return head;
}
What's going on here? There are only a couple of lines that differ, and to me it seems like they should be doing the same thing.