I have a struct as follows:
struct Node{
int *arr;
int *sol;
struct Node *Next;
};
i am creating Node in this way:
Node* MyNode = (Node *)malloc(sizeof (struct Node));
MyNode->arr = malloc(sizeof(int)*N);
MyNode->sol= malloc(sizeof(int)*N);
I then add MyNode to a linked list. How can i free memory for an element in the list.
is this correct:
pop(){
free(first->arr);
free(first->sol);
first=first->Next;
}
For any
structto be a node in alinked-list, you need aself-referential structure variablewhich should be declared asstruct Node *next;To allocate memory for a node of a linked-list, you need the following:
If you are not sure about the operations that you need to perform to delete node in a linked-list, you can use a
tempvariable to do the same in an easier way.Thumb rule for
free– for everymalloc()there should be afree()OTOH, to go through various scenarios in deleting a node in an linked-list, please refer this link.