Need some help in serialization [closed]
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 point开发者_高级运维er ? Also how can I avoid fragmentation ?
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 ---+
精彩评论