/**** * * This module defines a list specialization for nested lists of integers. It * does so by defining a specialized list element data type, and specialized * list construction and access functions. * * The specialized list element has the following concrete structure: * * |-------------------------------| * | int data | * |-------------------------------| * | List* sublist | * |-------------------------------| * * The value of the sublist field serves as the tag for the type of data that's * in an element. Namely, if the sublist field is null, then the int data * field holds an integer value. If the sublist field is non-null, then it * points to a sublist. * */ #ifndef nestedIntListIncluded #define nestedIntListIncluded #include "list.h" /* * NestedIntListElemData is the two-element struct that represents the data * stored in each list element. */ typedef struct { int data; List* sublist; } NestedIntListElemData; /* * NewIntDatum allocates a new integer-valued list element. I.e., it wraps a * plain int with a NestedIntListElemData. */ NestedIntListElemData* NewIntDatum(int i); /* * NewSubListDatum allocates a new list-valued element. I.e., it wraps a * sublist pointer with a NestedIntListElemData. */ NestedIntListElemData* NewSubListDatum(List* l); /* * EqualsFunc compares two wrapped list elements for equality. If the sublist * fields are both null, then it compares the int data values. Otherwise in * recursively compares two sublist values. */ bool EqualsFunc(void* d1, void* d2); /* * PrintFunc is used as the second arg to PrintList to print out nested int * lists. As above, it uses the value of sublist data field to figure out what * to do. If sublist is null, it prints the element value as an int. * Otherwise it recursively prints the element as a sublist. */ void PrintFunc(void* d); /* * CopyFunc is used as the second arg to CopyList. It has the same * base/recurse logic as EqualsFunc and PrintFunc. */ void* CopyFunc(void* d); #endif