8.3. Self-Referential (linked list)ΒΆ
It is common to have one item in a structure be a pointer to another data structure of the same type. This type of structure can be generally classified as a linked list. If the pointer does not point to another structure, we set the pointer to be NULL.
typedef struct list { int data; struct list *next; } LIST;
We can visualize the data as a chain of data structures.
----------------- -------- -------- start-->| data | *next -|-->| d | -|--->| d | -|--->NULL ----------------- -------- --------
One can easily develop a whole library of routines to do various manipulations on linked list type data structures. Some examples are: insertion, deletion, sorting, printing, counting, saving and reading from a file. Here is a simple function to append a new value to the end of our list. The class construct in C++ and Java is a direct descendent of C data structures which have a library of routines associated with the data structure.