Save linked list in binary file

//deleted
Last edited on
Would probably need a constructor that can take two iterators so that when you actually read the file, you can just pass these values into the list via iterators to the filestream. As for the actual saving of the list into the file, you could probably implement this via exception handling, such that if you have to crash or exit due to a program error (not a PC crash), then an exception is thrown, and the list is traversed (in O(n) unfortunately) and all the elements are added to the file.
Last edited on
If you added a save method that looks a lot like the display, except it writes to a file to a stream instead, that might handle the write.
1
2
3
4
5
6
7
void LinkedList::saveOn(std::ostream& os) {
    Node* temp = head;
    while (temp != NULL) {
        os << temp->data;
        temp = temp->next;
    }
}


Then you can write a load method that does the converse.
Topic archived. No new replies allowed.