Solved

linked list

Posted on 2004-04-18
5
308 Views
Last Modified: 2013-12-14
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
Comment
Question by:aajjaa
  • 3
  • 2
5 Comments
 
LVL 12

Accepted Solution

by:
stefan73 earned 50 total points
ID: 10857446
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
 

Author Comment

by:aajjaa
ID: 10864392
Thank you Stefan! that's exactly what I needed to know
0
 

Author Comment

by:aajjaa
ID: 10864651
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
 
LVL 12

Expert Comment

by:stefan73
ID: 10877013
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
 
LVL 12

Expert Comment

by:stefan73
ID: 10877018
Your iterator use is correct.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PDF library for Delphi 2 103
Change local server setting in php 6 81
Better understanding on C++ Class serialization and formats 9 59
gdb doesn't stop on breakpoint 2 45
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now