O'Reilly logo

Just Enough C/C++ Programming by Guy W. Lecky-Thompson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Destroying the List

Finally, you come to the clean-up function. When you have finished manipulating the list, you need to dispose of it in a way that returns the memory that you allocated for the data and pointers back to the operating system. The standard companion function to malloc is called free.

You need to traverse the list and dispose of each node in turn. Naturally, there are a few different ways to do this, but this section concentrates on a recursive approach because it illustrates this important technique.

Code Sample 13.8: Recursive Linked List Destruction

Recall that you need a way to stop the recursion as well as a call to the function to continue until such a time as that condition is met. The following is a suggested approach: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required