![]() |
| |||||||
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 |
| Guest
Posts: n/a
| 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 |
| and the hat of Jobseeking 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;
}
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 | |
| | #3 |
| 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) ); |
|
| | #4 |
| and the hat of Jobseeking 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 | |
| | #5 |
| 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. |
|
| | #6 |
| and the hat of Jobseeking 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |