Dynamic number of linked lists
I am creating a program that will maintain a dynamic number of doubly linked lists, (This example is obviously a singlely LL). Each list will contain similar data, yet they are not related and so are to be treated seperately. At runtime I will be giving the user the option to select from a list the particular linked list they wish to examine.
I have two questions:
1. After declaring a pointer to a node which is to be my root node for a linked list - what is the best way to initialise this newly allocated memory given the following structure?
2. It occurs to me that I may need a linked list to maintain the dynamic number of root nodes. Each node pointing to the address of each root node - or is this an over complication. Any ideas?
3. (secret third question) Just out of interest, rather than exiting from the program when memory cannot be allocated, what does everyone else do to handle these kind of situations - to handle the exit a little more gracefully?
Code:
#include <stdio.h>
typedef struct NODE
{
struct NODE *link;
int value;
} Node;
int main(void)
{
Node *root;
root = (Node *) malloc(sizeof(Node));
if (root == NULL)
{
printf("Error in allocating memory");
exit(1);
}
// Is this correct?
root = NULL;
return 0;
}
Thanks all! :)