C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-19-2001, 01:00 PM   #1
Unregistered
Guest
 
Posts: n/a
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.
  Reply With Quote
Old 11-19-2001, 01:37 PM   #2
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,647
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
Salem is offline   Reply With Quote
Old 11-19-2001, 01:54 PM   #3
Unregistered
Guest
 
Posts: n/a
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) );
  Reply With Quote
Old 11-19-2001, 02:38 PM   #4
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,647
> 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.
Salem is offline   Reply With Quote
Old 11-20-2001, 08:08 AM   #5
Unregistered
Guest
 
Posts: n/a
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.
  Reply With Quote
Old 11-20-2001, 12:48 PM   #6
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,647
If you do a board search for linked list, I'm sure something will show up, it seems to be discussed often enough.
Salem is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting linked list please help with CODE scarlet00014 C Programming 3 09-27-2008 11:24 PM
Problem with linked list ADT and incomplete structure prawntoast C Programming 1 04-30-2005 01:29 AM
airport Log program using 3D linked List : problem reading from file gemini_shooter C Programming 3 03-04-2005 02:46 PM
Linked List of Linked list of linked list : problem with a condtion gemini_shooter C Programming 6 03-02-2005 02:45 AM
How can I traverse a huffman tree carrja99 C++ Programming 3 04-28-2003 05:46 PM


All times are GMT -6. The time now is 03:57 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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