Original Post
Hello, I am trying to work on something that dynamically creates a linked list, all the tutorials I've seen show a way of doing it something like;
struct example {
char *whatever
struct example *next
};
struct example *teststruct = malloc( sizeof( struct example ) );
teststruct->next = NULL;
struct example *anotherstruct = malloc( sizeof( struct example ) );
teststruct->next = anotherstruct;
and then to cycle through it, just do;
teststruct = teststruct->next;
However, if I have to go everywhere creating structures and linking them manually like that I don't really see a point. So I was thinking of writing something that does this for me. Like;
struct example {
char *whatever;
struct example *next;
};
//Create nodes
void crea_node( struct example *tempstruct, int numnode )
{
for( int a = 0; a < numnode; a++, tempstruct = tempstruct->next ) {
struct example *creanode = malloc( sizeof( struct example ) );
tempstruct->next = creanode;
}
}
int main( void )
{
struct example lamestruct;
crea_node( &lamestruct, 5 ) // 5 linked lists
return 0;
}
Please note that was just a rough example, so of course syntax and things might be off. My question would be can something be created which roughly accomadates this idea? Doesn't have to be this exact implementation.