...

/

Helper Functions for Genericity

Helper Functions for Genericity

Learn to abstract the concrete handling of data types in order to keep the functions generic.

Introduction

The other essential function we need to write is the printGenericList function. We can’t view the contents of the lists, and we can’t check if we are building them correctly or not without a print function.

However, as we will see in this lesson, creating a generic print function isn’t easy. It will have to call printf with the concrete type of the element. But the whole point of a generic function is that it doesn’t care about the type of elements.

Printing elements from a generic linked list

To address the above issues, we’ll use a helper function which we will pass to the generic function as an argument. The helper function will be different for each data type:

  • For a list of integers, we may write a printInteger function, which knows how to print one integer.
  • For a list of
...