1 2 3 4 5 6 7 8 9 10 11 12 13
|
else {
current = (struct member_account *)malloc(sizeof(struct member_account));
strcpy(current->member_last,last);
strcpy(current->member_first,first);
current->member_balance=balance;
current->next=list;
}
while(list!=NULL){
current=list;
list=list->next;
current->next = new_list;
new_list= current;
}
|
So, you're adding it to the head of the list and then trying to move it to the end? Save yourself the trouble and don't try to do that.
At least, that's what I
think you're trying to do with that
while
loop. What it actually does is it breaks your list because you
malloc
a
new_list
node and then set
current->next
to this (uninitialized) node in the first iteration. Then, after the second iteration, the second node in the list (before the call to this function) points back to the old head of the list which is now pointing to an uninitialized node. If the loop doesn't continue after this point, you'll have two nodes which have no next pointers pointing to them (hence a broken list).
Instead, in the
else
, you should start with a pointer (call it foo) pointing to
list
. Then, keep checking
foo
's
next
pointer and update
foo
to be
foo->next
until
foo->next
is NULL. Then, set
foo->next
to be the new node.
Or, like I said, you can maintain a
tail
pointer which points to the last node in the list. If you have that, then you don't have to navigate to the end of the list. You can simply set tail->next to be the new node and then set tail to point to the new node.
1 2
|
return(new_list);
count3++;
|
And two things here: (1)
count3
is passed by value so it won't have any effect on the caller's copy of count3, and (2) it's being updated after the return statement so it won't do anything regardless.