954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

deleting an entire linked list of structs

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);

}
vincenzorm117
Newbie Poster
23 posts since Feb 2010
Reputation Points: 6
Solved Threads: 1
 

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

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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);
    
}
vincenzorm117
Newbie Poster
23 posts since Feb 2010
Reputation Points: 6
Solved Threads: 1
 

Nope. You only have step 5.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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

vincenzorm117
Newbie Poster
23 posts since Feb 2010
Reputation Points: 6
Solved Threads: 1
 

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

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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

sorry711
Newbie Poster
Banned
2 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Infraction Points: 10
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: