How to create a heterogeneous list of links in c or C ++

A list of links, which can contain float, integer, character and etc data and an algorithm, should be good and not very complicated

I was thinking of creating a structure with a void pointer that will point to subsequent nodes. but the problem is that I cannot use patterns with structure.

going down to c, I have to check every character entered by the user to check if it is an integer, float or character or not. Then we can continue on

suggest an efficient algorithm / code

+4
source share
6 answers

If you want to do this yourself, you basically want to create an array or a linked list of elements that encode both the data and the data type. You can use a structure that includes a type indicator and a union of the various types that you want to process, and create an array or linked list of this structure:

typedef struct { int type_indicator; union { float f; int i; double d; void *p; char c; } } generic_item; generic_item generic_array[10]; 

I will leave this to you to come up with an appropriate enumeration for the type indicator and add a function pointer for your algorithm. If you need a linked list instead of an array, you will also need to add a generic_item *next pointer.

I have not studied the promotion options referenced by the other answers, but I will probably look there first before trying to overturn my own solution.

+6
source

Using boost :: variant or boost :: any . Depends on your needs.

+5
source

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 (/* whether the user entered a char, string, float, or int */ { case /* char */: current->next.item = malloc(sizeof(char)); current->next.type = CHAR; current->next.next = NULL; break; /* and so forth, for string, int, and float */ } 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.

+4
source

http://www.boost.org/doc/libs/1_48_0/doc/html/variant.html

(which, of course, mentions C / C ++ unions before explaining which boost options give you this)

+2
source

A heterogeneous linked list can be created using void * as a pointer to a data item:

 struct Node { Node * previous; Node * next; void * p_data; }; 

Before deploying a heterogeneous container, you might ask if you can change the design to use home containers instead.

+1
source

You could use such things, using, as you mentioned, some void pointers in connection with some more or less complex macros.

You can define a structure (or class) containing three pointers: next, prev (for the next and previous list item) and some kind of void* data . In addition, you can store a type for each list entry (which can be implemented using enum or something similar).

In addition, you can define a macro that - taking into account the list item - retrieves data and automatically passes it to the specified type:

 #define get_list_item(item, type) *(type*)(((item)->data)) 
0
source

Source: https://habr.com/ru/post/1385184/


All Articles