Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: Beginner
May 26, 1998

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.

DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date