I am implementing a list structure in C. My current function I am working on is a destructive append function that takes two lists and appends the second one onto the end of the first. Right now, it appears to be working without runtime errors and memory leaks and I think I have all my cases covered, but when I submit it to my testing server, it says it is not correct. I was wondering if anyone could see any flaws that could cause it to not do what I expect it to do.
The append function:
ilist iappend_destroy(ilist il1, ilist il2){
if(il1 == NULL && il2 == NULL){
free(il1);
free(il2);
return NULL;
}else if(il1 == NULL){
free(il1);
return(il2);
}else if(il2 == NULL){
free(il2);
return(il1);
}else{
ilist tmp = iempty();
ilist clone = il1;
while(il1 != NULL){
tmp = icons_destroy(il1->first, tmp);
il1 = il1->rest;
}
ilist tmpclone = tmp;
while(tmp != NULL){
il2 = icons_destroy(tmp->first, il2);
tmp = tmp->rest;
}
idelete(tmpclone);
idelete(clone);
return il2;
}
The declaration of the ilist structure:
struct ilist_ADT{
struct ilist_ADT *rest;
int first;
};
The icons_destroy function:
ilist icons_destroy(int in, ilist il){
if (il == NULL) {
ilist anewlist = malloc(sizeof(struct ilist_ADT));
anewlist->first = in;
anewlist->rest = NULL;
return (anewlist);
} else {
ilist previous = malloc(sizeof(struct ilist_ADT));
previous->first = il->first;
previous->rest = il->rest;
il->first = in;
il->rest = previous;
return il;
}
}
free()on a null pointer has no effect. – Cygal Feb 22 at 15:58