NOTE. This is a pure C-answer.
This data structure is what I would start with:
typedef struct heterogeneous_list { enum { CHAR, STRING, FLOAT, INT } type; void *item; struct heterogeneous_list *next; }
When I received the item from the user, I would save it in the list (assuming the current points are at the end of the list):
current->next = malloc(sizeof(heterogeneous_list)); case ( { case : current->next.item = malloc(sizeof(char)); current->next.type = CHAR; current->next.next = NULL; break; } current = current->next;
When navigating through a list, it is now easy to process what is in the list based on type. The following code assumes that current
is the current element in the list that is being scanned at the iteration (for a loop passing through the list):
char currentItemChar; char * currentItemString; float currentItemFloat; int currentItemInt; case (current->type) { case CHAR: currentItemChar = *((char*) current->item); // process a character break; case STRING: currentItemString = (char*) current->item; // process a string break; case FLOAT: currentItemFloat = *((float*) current->item); // process a float break; . . . };
What would i do.
source share