开发者

Need some help in serialization [closed]

开发者 https://www.devze.com 2023-01-04 02:16 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago.

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 ---+
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号