Question on linked lists

This is the dynamic array version of it need to change it to an linked list
please help thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void write_records(SalesRecordPtr head, int n)   // Save records to file
                                                      
{  char      outfilename[MAX_FILE_NAME + 1];   // Name of file for output records
   ofstream  out;                              // Output file stream

   cout << "\n\nI need the output file name." << endl;
   open_output(out, outfilename);              // Get file name & open file

   
   
   if (out.good())
   {  out << n << endl;       
      int i;
      for(i = 0; i < n; i++) 
      {  head[i].write(out);  
      }
      out.close();
   }
   else
   {  cout << "\a\nUnable to save data!" << endl;
   }
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
#include <list>

// substitute actual typename of course
void write_records(std::list<typename>& head)   // Save records to file
                                                      
{  char      outfilename[MAX_FILE_NAME + 1];   // Name of file for output records
   ofstream  out;                              // Output file stream

   cout << "\n\nI need the output file name." << endl;
   open_output(out, outfilename);              // Get file name & open file

   
   
   if (out.good())
   { 
     
      for(std::list<typename>::iterator pos(head.begin()), end(head.end); pos != end; pos++)
      {  
              (*pos).write(out);  
      }
      out.close();
   }
   else
   {  
                cout << "\a\nUnable to save data!" << endl;
   }


You could also make it a template function and pass two iterators so that it is more generic and supports other container types as well. I could make many other comments as well such as, prefer to overload operator<< instead of having a write member function for the object. Take a look at this article with regards to passing arrays or containers to functions. There are many formulas for accomplishing that depending on what your preferences are.
http://cplusplus.com/forum/articles/20881/
thanks
Topic archived. No new replies allowed.