0

When we write a linked list program in C++, It will give us all operations that we can perform on a linked list like insertion,deletion,search etc. But once we exit out of the program what happens to the created list then?

If we write all the data to a file then is there a way we can reconstruct the same linked list we created before we exited the program which will again give us all the operations in the same time complexities?

NoobLearner
  • 61
  • 1
  • 6
  • 1
    Yes you can store the data you have in memory on file. With the right format of the on-file data you can even store a linked list. How to create such a format is a to broad subject though. You could also look into the subject of [*serialization*](https://en.wikipedia.org/wiki/Serialization) and find a library to handle it for you (asking for libraries is off-topic though). – Some programmer dude Oct 23 '18 at 09:00
  • 1
    Thanks. So we can do that. I'll look into serialization. Good Day my friend~~ – NoobLearner Oct 23 '18 at 09:03
  • Possible duplicate of [How to read / write a struct in Binary Files?](https://stackoverflow.com/questions/5506645/how-to-read-write-a-struct-in-binary-files) – Bernhard Barker Oct 23 '18 at 11:44
  • See also: [How to write an object to file in C++](https://stackoverflow.com/questions/2376193/how-to-write-an-object-to-file-in-c) – Bernhard Barker Oct 23 '18 at 11:45

2 Answers2

1
  1. Once you exit the program linked list is lost and allocated memory is lost and it will be released to operating system.

  2. You can store the data of linked list in a file, when you run the program again you can read the data from the file and fill the newly created linked list. In the second run, the memory allocated to data will be different.

yadhu
  • 1,253
  • 14
  • 25
0

Yes, we can reconstruct the same linked list from the saved file. All you have to do is write the program is a way that

  1. Every time before exiting the program, the linked list should be written to a file.
  2. Every time when the program starts, the program should read the stored data from the file and create the linked list.

And in between, you can do other operations like inserting, removing, finding etc.

ksohan
  • 1,165
  • 2
  • 9
  • 23