Reputation: 1
I asked something similar earlier but now I have run into a different problem. Can you please look at it? I am trying to free the nodes in a linked structure created in my program. Here is the structure.
struct node {
unsigned x;
double y;
struct node *next;
};
typedef struct node Nodes, *This;
Declaration of the functions
void freeNodes(Nodes *a);
void freeThis(This *);
Function freeNodes
:
void freeNodes(Nodes *a)
{ free(a);
print ("success");
}
Function freeThis
:
I don't know how to fix this so I can free the nodes from the whole linked list.
void freeThis(This *p)
{
Nodes *tmp;
while (p != NULL)
{
(*tmp) = Nodes *p;
*p=(*p)->next;
freeNodes(tmp);
tmp = NULL;
}
}
Upvotes: 0
Views: 29
Reputation: 753605
I would not use the This
type, as I noted in a comment. It makes it harder (for me) to understand what is going on. (I don't suppose it makes it easier for you either, but I might be wrong about that.)
Given that it exists, then:
void freeThis(This *p)
{
if (p != 0)
{
Nodes *tmp = *p;
*p = NULL;
while (tmp != NULL)
{
Nodes *next = tmp->next;
freeNodes(tmp);
tmp = next;
}
}
}
Upvotes: 1
Reputation: 40145
try this
void freeThis(This *p){
Nodes *tmp;
if(!p) return;
while (*p != NULL){
tmp = *p;
*p=(*p)->next;
freeNodes(tmp);
}
}
Upvotes: 1