Thread: how to create a linked list of items using pointers stored in a two dimensional array

  1. #1
    Unregistered
    Guest

    Question how to create a linked list of items using pointers stored in a two dimensional array

    This is my first C program and the use of a linked list is new to me. The problem with this one is that it has to have pointers stored in a two dimensional array which at a later point will be pointing to different linked lists depending on what is been read from a file into the program. I can use an array fine as far as I know but the examples of linked lists I have seen so far don't quite fit with this specific task I want to accomplish. I have a program fragment which I have been trying to get to work but its very possible that I'm missing some very simple things that prevent it from working, I can compile it but it crashes everytime I run it.

    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <string.h>

    struct record {
    char item[15];
    struct record *link;
    };

    typedef struct record item_node;


    typedef struct {
    int length;
    item_node *list;
    } data_type;

    data_type infotable[3][3] = {{{0,NULL}, {32,NULL} {99,NULL} },
    { {32,NULL}, {0,NULL}, {86,NULL} },
    { {99,NULL}, {86,NULL}, {0,NULL} } };


    list_node *new_list;




    int main()
    {

    strcpy(infotable[0][1].list->item,"item1");

    infotable[0][1].list->link = NULL;

    strcpy(new_item->item,"item2");

    new_item->link=infotable[0][1].list;

    infotable[0][1].list=new_item;

    return 0;
    }

    I believe that this program is meant to provide an array (this is a temp array made to get this program fragment to work) with null elements that can be changed to pointers to linked lists, the lines of code in main are examples of how 2 itesm can be added to the array at [0][1]. I must be missing some important things since this program wont run at all, I cant actually compile the program with the malloc lines in either and im not too sure why, I havent looked into how to print the list from the array yet either.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well this is how you add one element to the list.
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct record {
        char item[15];
        struct record *link;
    }; 
    typedef struct record item_node;
    
    typedef struct {
        int length;
        item_node *list;
    } data_type;
    
    // a few missing ,
    data_type infotable[3][3] = {
        { { 0,NULL}, {32,NULL}, {99,NULL} },
        { {32,NULL}, { 0,NULL}, {86,NULL} },
        { {99,NULL}, {86,NULL}, {0, NULL} }
    };
    
    // there are no list_nodes here
    // list_node  *new_list;
    
    int main() {
        infotable[0][1].list = malloc( sizeof(item_node) );
        strcpy( infotable[0][1].list->item,"item1" ); 
        infotable[0][1].list->link = NULL;
        return 0;
    }
    Each node in the list needs a malloc to allocate space for the element you're adding to the list.

    Adding more elements is similar - but you need to decide whether you're adding elements
    - at the start
    - in the middle
    - at the end

  3. #3
    Unregistered
    Guest
    well it may be that my problems lie with the malloc line not working, when i try to complie the program fragment i get the error:

    Error E2034 :Cannot convert 'void *' to 'record *' in function main()

    this points to this line:

    infotable[0][1].list = malloc( sizeof(item_node) );

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Error E2034 :Cannot convert 'void *' to 'record *' in function main()
    That's because you're compiling this 'C' code with a C++ compiler.

    Make sure your source code ends with .c, not .cpp or whatever.

  5. #5
    Unregistered
    Guest
    Thanks for your help Salem, my problems so far have been solved but I'm still having trouble finding any resource that has examples of what I'm trying to do which isn't helping much, I'm not sure how to continue the linked list I have in this prgram or how to print it all out in order, I don't need to be able to add or subtract from the list once the program is running unless the whole list is going to be replaced by an input from a file, I'm going to code this input later on.

    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <string.h>

    struct record {
    char item[15];
    struct record *link;
    };

    typedef struct record item_node;


    typedef struct {
    int length;
    item_node *list;
    } data_type;

    data_type infotable[3][3] = {{{0,NULL}, {32,NULL}, {99,NULL} },
    { {32,NULL}, {0,NULL}, {86,NULL} },
    { {99,NULL}, {86,NULL}, {0,NULL} } };


    list_node *new_list;




    int main()
    {
    infotable[0][1].list=malloc(sizeof(item_node));

    strcpy(infotable[0][1].list->item,"item1");

    infotable[0][1].list->link = NULL;

    new_item=malloc(sizeof(item_node));

    strcpy(new_item->item,"item2");

    new_item->link=infotable[0][1].list;

    infotable[0][1].list=new_item;

    printf("%s\n", new_item->link);

    printf("%s\n", new_item->item);

    return 0;
    }


    This prints 'item1' 'item2' as i expected, but im not sure how to get the next items in the list added or how to print them or if im trying to print them in the correct way in the first place. I would also like to code this in a way that will be easy to alter later on when i want to replce the lists in the array with new ones stored in a file. Any help is appreciated.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you do a board search for linked list, I'm sure something will show up, it seems to be discussed often enough.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 11:24 PM
  2. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  5. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM