tags:

views:

133

answers:

1

I am trying to write a program to serialize a linked list to a file without using any libraries. My problem is how to add or remove nodes to the serialized structure since I dont have next pointer ? Also how can I avoid fragmentation ?

Thanks & Regards

+5  A: 

If your linked list doesn't have loops, then the fact that this is a "linked list" is a memory detail, not a serialization detail. Just write the node values out into the file and build the next pointers when you deserialize.

However, if your linked list does have loops, then you'll need something smarter. You'll need to store next pointers as a file offsets to the node (or something similar) to encode the "link".

For each node in your linked list, store two words. The first is the data, the second is the offset of the next node. Here is an illustration of the circularly linked list:

 +-> 1234 -> 5678 -> 2398 -+
 |                         |
 +-------------------------+


0  : 4bytes: 1234 : int data  <------------+
4  : 4bytes:    8 : offset of next node -+ |
                                         | |
8  : 4bytes: 5678 : int data  <----------+ |
12 : 4bytes:   16 : offset of next node -+ |
                                         | |
16 : 4bytes: 2398 : int data  <----------+ |
20 : 4bytes:    0 : offset of next node ---+
Stephen
@Stephen Thank you very much. But how can I store them in sorted order ?
mousey
@mousey : You could sort them before serializing. Typically, you don't want your serialization routines to modify your data. In other words, you want `A == Deserialize(Serialize(A))`. If you start changing the order of elements, you're in trouble.
Stephen
@Stephen If I add a new element that need to be in sorted order. do I need to deserialize the entire list for adding a new element ?
mousey
@mousey : Probably. But, typically you don't serialize (and deserialize) every time you add an element to a list. You usually serialize it to send it over the network, store it on disk, etc. You may choose a different format altogether if the sorted property (and constant re-encoding) is important - when writing, always append to the back of the file and keep it sorted when you read it back in.
Stephen
Also I have an another question. How to store the pointers ?
mousey
since if I store the pointers there is no meaning
mousey
@mousey : A pointer is just an address. You can't really store the pointers meaningfully. You can store 'pointers' to within the file by using file offsets. For example, `node->next` is at byte234 in the file, so seek there to read it. However, you'll need to deduplicate these references. You sound like a beginner, so consider if you really need this behavior.
Stephen
No need to use anything as drastic as file offsets. Just a plain sequence number will do. That is, store each node as (conetent, next) where next is simple the number of nodes into the serialized list of the node references. If it hasn't been done yet, give it the next number and do it next. Simple. Trees are a little harder: use the 2n and 2n+1 ordering.
dmckee
@dmckee : good call, straight sequence numbers will be easier than offsets.
Stephen
@dash-tom-bang : yes... I was changing my example as I wrote it... forgot to update that. thanks.
Stephen