• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 319
  • Last Modified:

linked list

struct Data
{
   char name[51];
   int size;
   int num;
   char r[51];
   char a[51];
   char x[51];
};

The information is read from a file and stored in the structure using
while(!file.eof())
{file.read(reinterpret_cast<char*>(&f),sizeof(f));}

I need help on how to store the information from the struct to a linked list.

Thanks
0
aajjaa
Asked:
aajjaa
  • 3
  • 2
1 Solution
 
stefan73Commented:
Hi aajjaa,
Use the list template of STL:

#include <list>
typedef list<Data> DataList;
DataList myDataList;
myDataList.push_back(f);

...and voilĂ , your data is in a linked list.

Cheers,
Stefan
0
 
aajjaaAuthor Commented:
Thank you Stefan! that's exactly what I needed to know
0
 
aajjaaAuthor Commented:
Now that I have all the elements in my linked list, if I want to sort them by name.. I was wondering if it is better to use vectors instead of list?  I know there's a sort function for vectors, I'm not familiar with one for list. Can you please advice me on what to use?

With this data, I will also need to find a specific name, r, a, or etc in the list.. I'm planning in using the following code:
list<Data>::iterator iter;
iter=find(DataList.begin(),DataList.end());
if(iter!=DataList.end())
{ //found }
else
{ //not found }

Is this OK?

Thanks!!
0
 
stefan73Commented:
A linked list is not good for sorting or searching.

Maybe you should use a vector or a map, depending on whether you need sorting or fast search more often.
0
 
stefan73Commented:
Your iterator use is correct.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now