writing linked structures

This is a discussion on writing linked structures within the C Programming forums, part of the General Programming Boards category; how would you go about writing linked structures to the hard drive and reading them as well?? I can't think ...

  1. #1
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501

    writing linked structures

    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.

  2. #2
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    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

    Code:
    node *current=&headnode;
    
    while(current)
    {
       cout<<current->data<<endl;
       current=current->next;
    }
    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.
    Last edited by confuted; 07-05-2003 at 02:57 PM.
    Away.

  3. #3
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    i will give that a shot.

  4. #4
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    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.

  5. #5
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    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.

  6. #6
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    i got it now, thanx, still don't know why he thought this was c++.

  7. #7
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    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.

  8. #8
    King of the Internet Fahrenheit's Avatar
    Join Date
    Oct 2001
    Posts
    128
    This is the code I use for LL. Wrote it for an IRCd i'm working on.

    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 -- ; 
    }
    What you do is put LL_LEAF as the first member of all your structs that will act as leafs... example.

    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 ;
    }
    Do what you will.

  9. #9
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    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.

  10. #10
    King of the Internet Fahrenheit's Avatar
    Join Date
    Oct 2001
    Posts
    128
    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.

  11. #11
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    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.
    I actually laughed when I read that. Out loud. You obviously don't know what you are talking about at all.

    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.

  12. #12
    King of the Internet Fahrenheit's Avatar
    Join Date
    Oct 2001
    Posts
    128
    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.

    You are quick to flame things that are different than the traditional method.
    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.
    Last edited by w00tsoft; 07-06-2003 at 12:34 AM.

  13. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Singly Linked Lists: Clarification Needed
    By jedispy in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2006, 04:30 PM
  2. C Linked Lists and Structures help!
    By Yorae in forum C Programming
    Replies: 0
    Last Post: 03-07-2006, 08:55 AM
  3. How to use Linked List?
    By MKashlev in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 07:11 AM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM

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