I have been having trouble developing a function that frees all of the memory associated with structs of a linked list. Im not asking for a clear cut answer, just an idea or a function that can aid.

F.Y.I. the linked list contains a head struct and a tail struct to identify the

Here's some code that might spark some thought:

void deleteAllStructs(void){
struct structTypeName   listingPtr;
do{
for(listingPtr=gHead;listingPtr->next!=NULL;listingPtr=listingPtr->next)
;
listingPtr = NULL;
while(gHead!=NULL);

}

Edited 5 Years Ago by vincenzorm117: n/a

1) Start at head to get address of first node -- nodeA

2) Get address of next node -- NodeB.
3) Delete nodeA
4) Move nodeB into nodeA
5) Loop back to 2

Is this what you mean:

struct CarInfo{
    char make[kSmall];
    char model[kSmall];
    char year[kYear];
    char engine[kSmall];
    int carNumber;
    
    struct CarInfo *next;    
};

void  deleteAllStructs(void){

    struct CarInfo *erasingPtr;
    
    do {
        erasingPtr=gHead->next;
        gHead=NULL;
    } while (NULL!=gTail);
    
}

I probably misinterpreted what you typed, but I think I have until step 3 I just wasn't specific:

Heres the situation:

struct CarInfo{
    char make[kSmall];
    char model[kSmall];
    char year[kYear];
    char engine[kSmall];
    int carNumber;
 
    struct CarInfo *next;    
};

You can assume that the address of the first node that being the head is always available with the struct ghead being a global struct (same with gtail) and the next address is ghead->next (check the struct type declaration above)
6 of these of structs have been linked in a list as such:

1.ghead
2.ghead->next
3.ghead->next->next
4.ghead->next->next->next
5.ghead->next->next->next->next
6.ghead->next->next->next->next->next or gtail
7.gtail->next (which is NULL)

Then the following function should perform the third step unless you meant something else by "deleting nodeA". Should I use the free() function?

void  deleteAllStructs(void){
 
    struct CarInfo *erasingPtr;
 
    do {
        erasingPtr=gHead->next;
        gHead=NULL;
    } while (NULL!=gTail);
 
}

Im not sure what you mean in steps 4 and 5

Edited 5 Years Ago by vincenzorm117: n/a

I probably misinterpreted what you typed, but I think I have until step 3 I just wasn't specific:

void  deleteAllStructs(void){
 
    struct CarInfo *erasingPtr;
 
    do {
        erasingPtr=gHead->next;
        gHead=NULL;
    } while (NULL!=gTail);
 
}

Point to the line that accomplished step 1:
1) Start at head to get address of first node -- nodeA

Point to the line that accomplished step 2:
2) Get address of next node -- NodeB.

Point to the line that accomplished step 3:
3) Delete nodeA

Point to the line that accomplished step 4:
4) Move nodeB into nodeA

Point to the line that accomplished step 5:
5) Loop back to 2

Oh? This is new to me. I do not know about it. You did very well. Wish you a nice day.

This article has been dead for over six months. Start a new discussion instead.