Deallocating space used by our queue
It's considered a good practice to always write a function that
deallocates
all space used in our own user-defined dynamic data structures.
In the case of our queue, we need to deallocate 3 things:
- the memory required for the data in every element,
- the memory required for every element,
- the queue itself.
void queue_free(QUEUE *q)
{
ELEMENT *this, *save;
this = q->head;
while( this != NULL ) {
save = this;
this = this->next;
free(save->data);
free(save);
}
free(q);
}
QUEUE *my_queue = queue_new();
....
// use my local queue
....
queue_free( my_queue );
|
CITS2002 Systems Programming, Lecture 19, p15, 3rd October 2023.
|