Saving LinkList on disk and then reading it back

I have created a simple link list and would like to save it on disk and then read it back. What is the best way to do so? What if I have a huge list, and I only want to read a part of it from the disk?

This is inherently problematic. While there are designs for disk-based linked lists, the type of linked lists you normally have in memory do not normally transfer well to disk.

As long as you aren’t doing anything unusual, however, you can simply write one node at a time to the file. Any Next/Prev fields would no longer be valid once on disk. To read it back, you would need to read each node (you might set any Next/Prev fields to NULL for safety) and then call your function that appends a node to the end of the list.

Certainly not as convenient as saving arrays, but that’s not too much work considering the benefits that can come from linked lists.

As far as reading back only a portion of the list, that really depends on what you are doing with it. If the list is too big to fit in RAM, maybe you are better off with a disk-based design where only an index resides in computer memory.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


Recent Articles: