how would you go about writing linked structures to the hard drive and reading them as well?? I can't think of a way to do it.
how would you go about writing linked structures to the hard drive and reading them as well?? I can't think of a way to do it.
I assume you mean a linked list. To write them to the HD, (file format wise) you can use fwrite() (check msdn for info on how to use it). To print the data to the screen, check out this psuedo-code. Well, I guess it's more just code where you get to change the variable names
Edit: If your last node's next isn't NULL, I will keel j00! And this code won't work properly. Initialize your pointers to NULL, boys and girls.Code:node *current=&headnode; while(current) { cout<<current->data<<endl; current=current->next; }
Last edited by confuted; 07-05-2003 at 02:57 PM.
Away.
If you've learned about classes, I would suggest making your linked list nodes into a class. Here's what a basic prototype could look like - I'll leave the coding to you.
Code:class bytes { public: void adddata(unsigned char add); //this function adds data to the list bytes(void); //constructor ~bytes(void); //destructor void printlist(); //prints list...quite optional private: unsigned char data; //Piece of data bytes *next; //pointer to the next node in the (unsorted) linked list }; //I'll throw in a basic adddata function for you too, just so you can see it bytes::adddata(unsigned char add) { //this function is called from the head node, so //"this" points there bytes *current=this; while (current->next) { current=current->next } current->next=new *bytes; if (current->next) current->next->data=add; }
Away.
c doesn't have classes like c++. <edit>You got to tell him it was a c board before i did</edit>
BTW, I got the code working. thanx.
i got it now, thanx, still don't know why he thought this was c++.
Haha, forgot that I was on the C board. Oh well. Some of what I said is still valid :P For example, suggesting fwrite(), although I didn't give example code for it.
Away.
This is the code I use for LL. Wrote it for an IRCd i'm working on.
What you do is put LL_LEAF as the first member of all your structs that will act as leafs... example.Code:typedef struct ll_leaf { struct ll_leaf * next ; struct ll_leaf * prev ; } LL_LEAF ; typedef struct ll_hub { LL_LEAF * first ; unsigned int count ; } LL_HUB ; void ll_init (LL_HUB * hub) { hub->first = 0; hub->count = 0; } void ll_add (LL_HUB * hub, LL_LEAF * leaf) { leaf->prev = 0 ; if (!hub->first) { hub->first = leaf ; leaf->next = 0 ; } else { leaf->next = hub->first ; hub->first = hub->first->prev = leaf ; } hub->count ++ ; } void ll_del (LL_HUB * hub, LL_LEAF * leaf) { if (hub->first == leaf) hub->first = leaf->next ; else { leaf->prev->next = leaf->next ; if (leaf->next) leaf->prev->next->prev = leaf->prev ; } hub->count -- ; }
Do what you will.Code:typedef struct blah { LL_LEAF ll ; char * what ; } BLAH ; BLAH a, b, c, * leaf ; LL_HUB hub ; ll_init(&hub) ; a.what = "Hi" ; b.what = "Hello" ; c.what = "Why, hello there, sir!" ; ll_add(&hub,(LL_LEAF*) &a) ; ll_add(&hub,(LL_LEAF*)&b) ; ll_add(&hub,(LL_LEAF*)&c) ; ll_del(&hub,(LL_LEAF*)&b) ; leaf = hub.first ; while (leaf) { puts(leaf->what) ; leaf = leaf->ll.next ; }
That is horrible. Do not use it. w00tsoft, do you understand the purpose for a linked list? New nodes should be dynamically allocated - that's why you use a linked list in the first place. All you're doing is creating a really inefficient array with severe limitations - it looks like a linked list, but it's useless for real work. Not to mention the complete lack of descriptive variable names in your "example," which is going to confuse the crap out of anyone who doesn't already know what they are doing with a linked list.
Away.
It works quite well for me, but of course I made the LL_LEAF beginning of the structure so it would save an extra call to malloc in my application. The reason to use a linked list isn't so new nodes will be dynamically allocated; it's because it's a much more efficent method of storage if you do multiple adds or deletes. The code I pasted manages to add and delete without looping through the list at all. You are quick to flame things that are different than the traditional method.
I actually laughed when I read that. Out loud. You obviously don't know what you are talking about at all.Originally posted by w00tsoft
It works quite well for me, but of course I made the LL_LEAF beginning of the structure so it would save an extra call to malloc in my application. The reason to use a linked list isn't so new nodes will be dynamically allocated; it's because it's a much more efficent method of storage if you do multiple adds or deletes. The code I pasted manages to add and delete without looping through the list at all. You are quick to flame things that are different than the traditional method.
Your code doesn't add a new node unless it is statically declared already and your ll_add() function is passed the address. That's not what you're supposed to do with a linked list. You could allocate the memory dynamically in some other function, of course, turning it into a legitimate linked list, but it would be a lot better idea to allocate it in your ll_add() function. A linked list is made so that you can have a variable number of nodes - for when you can't tell how many you'll need at run time when you are coding it. When you "delete" a node in a linked list, when you do it properly, you're deallocating the memory for it, and when you add a node, you're allocating the memory for it. In both instances, you set the pointers on other nodes accordingly. Gah, go read a linked list tutorial, I'm not going to write one for you.
Away.
I'm happy you find humor in my posts. My IRCd code requires that I malloc a structure for my information, and then your "definition" of a linked list requires that I malloc a node. I wrote the code to eliminate this useless extra call to malloc.
EDIT: Maybe if I made the example malloc, you would see what i'm talking about. It's late, so I decided to just use static variables. Should work either way.You are quick to flame things that are different than the traditional method.
Last edited by w00tsoft; 07-06-2003 at 12:34 AM.
Using fwrite/fread has already been suggested as an option. Please be aware that this solution is inherently not portable. That is, it will work as long so you are using the same OS, compiler, and compile options; beyond that, results may vary.
If you want something portable, also consider using fprintf/fscanf. This suggests you write to and read from a file as text. This can also have the advantage of being "human readable/maintainable". See also Q2.11 and Q20.5.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*