reading and writing a link list from file

i am trying to make a simple project .
which can read and write a data in form of linklist form file.
i searched alot but i dont make any sense, i need algorithms .
from where i can learn this thing ....???
closed account (SECMoG1T)
let me give you an example on how you could achieve that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
struct Node/// will represent our nodes.
{
   int data;
   Node* next;
   Node(int value,Node* ptr): data(value), next(ptr){}
};

class list/// will manage our list.
{
    public:
      list(): root(nullptr),last(nullptr){}
      Node* getroot() const {return root;}
      void add_nodes(ifstream& in);
     ///more functions
   private:
     Node* root;
     Node* last;
};



void list::add_nodes(ifstream& in)
{
   1. if the stream is aleady attached to a file check if it is in an error state else open
       your file.
   2. while your file is open and no error state is set read values from your file 
       and add them to your list.
      
      for my case i would do something like this inside a loop
      
            in>>current_data; ///current_data is a temporal int variable am using to read data
                                          /// from file.
           /// to add the first node i would do something like
               root=last= new Node(current_data, nullptr); last=last->next;
            //// adding subsequent nodes you'll just need to change the last pointer if the
            ///  order of your values isn't really important, if it matters you could create a 
            /// sorting function or might be develop a more complex function thet adds your 
           /// values at their right positions

              last=new Node(current_data,nullptr); last=last->next;
   3. continue reading till you hit the end of file marker, close you'r stream and you are     
       done.
 } 



develop your idea from there.
Last edited on
tnx dear andy
Topic archived. No new replies allowed.