array of linked list

This is a discussion on array of linked list within the C Programming forums, part of the General Programming Boards category; hi! i'm trying to read into a dynamic array of linked lists. where the info is: 0: a1 a2 a3 ...

  1. #1
    Registered User
    Join Date
    May 2007
    Location
    Australia
    Posts
    3

    array of linked list

    hi!
    i'm trying to read into a dynamic array of linked lists.
    where the info is:

    0: a1 a2 a3
    1: b a1 a2 a3
    2: l1 l2 l16

    i want to put 0:, 1:, 2: into a dynamic array.
    and i want to put a1, a2, a3 and b etc into a linked list.

    i havent any problem with doing the linked list part...

    Code:
    list_t *list;
    int   i = 0;
    int   a=0;
    int MAXNO=10;
    
    list = make_empty_list()
    while(scanf("%d", &i)==1){
              for(a=0; a<MAXNO; a++){
                           list= insert_at_head (list, i);
              }
    }
    but how do i create the dynamic array with the linked list?
    which gets created first?
    how does it allocate?
    does this mean i need a buffer and i need to read in a line at a time?


    Thanks for your help

  2. #2
    Registered User
    Join Date
    May 2007
    Posts
    88
    If I understand your question correctly, you are wanting to make an array of linked lists. In which case, you can create it like this:

    Code:
    list_t **list_array = (list_t**) malloc(sizeof(list_t*) * max_size);
    list_array[0] = root_ptr;
    //etc...
    Basically, this is just an array of root pointers. It doesn't matter which one is constructed first. Hope this helps.

  3. #3
    C 1337 Meshal's Avatar
    Join Date
    Nov 2006
    Posts
    70
    try to not use casting so it will be like this :
    Code:
    list_t **list_array = malloc(sizeof(list_t*) * max_size);
    to be a 1-1, learn C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  2. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 12:21 PM
  3. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  4. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 04:16 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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