devxlogo

Saving LinkList on disk and then reading it back

Saving LinkList on disk and then reading it back

Question:
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?

Answer:
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.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist