Dynamic number of linked lists

This is a discussion on Dynamic number of linked lists within the C Programming forums, part of the General Programming Boards category; I am creating a program that will maintain a dynamic number of doubly linked lists, (This example is obviously a ...

  1. #1
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343

    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!
    Last edited by foniks munkee; 02-23-2002 at 10:06 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    > It occurs to me that I may need a linked list to maintain the dynamic number of root nodes
    Seems like a good plan to me

    If you think about it, and do it right, you can re-use the same code for both the master list, and each sub-list. This has to be better than crafting two approaches.

    > to handle the exit a little more gracefully?
    Fairly simple list walker will do the job
    http://www.eskimo.com/~scs/C-faq/q7.24.html

  3. #3
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    If you think about it, and do it right, you can re-use the same code for both the master list, and each sub-list. This has to be better than crafting two approaches.
    Agreed. I have a function that will work for any list and that contains any data type.. so that should do the trick. By using the one function which accepts a pointer to a comparison function (rather than incorporating it in the insert function) and also traverses the list and inserts.. I get a lot of flexibility.

    As for the answer to my first question, I know that the root = NULL is BAD. I was going to use this solution:

    memset(root, NULL, sizeof(Node));
    Last edited by foniks munkee; 02-25-2002 at 01:08 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. One more question about linked lists?
    By shaeng in forum C Programming
    Replies: 2
    Last Post: 06-24-2003, 07:36 AM
  2. Replies: 2
    Last Post: 01-18-2003, 12:32 AM
  3. Linked lists and file i/o, and some other stuff
    By ninja in forum C++ Programming
    Replies: 9
    Last Post: 05-19-2002, 07:15 PM
  4. dynamic memory + linked lists
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 02-10-2002, 03:50 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21