I think you are using other library for this? Do you have another way without using other library... below is an example of writing a linked list to a file.. But I am unable to do the same for reading a file and put ir in a linked list... Thanks in advance...
reading in should just be along the lines of
tmp = new node;
file >> (*tmp).fields; //all the fields like above
insert tmp into list. Preferably at the top each time:
tmp->next= head;
head = tmp;
this reverses the data but its a lot less aggravation. if you feel compelled to insert at the end, make sure your list keeps a tail pointer and work it from there
tmp -> next = null;
tail -> next = tmp;
tail = tmp;
struct Node
{
int data; // for example
Node *pNext; // pointer to next node
};
void foo()
{
Node *linkedList = NULL;
while (/* some condition */)
{
Node *node = new Node;
int data;
// Read data from file . . .
// assign its value to the current node's data
node->data = data;
node->pNext = linkedList; // this adds the current node
linkedList = node; // to the beginning of the list
}
// now you have a linked list which you could iterate through like this
Node *iterator = linkedList;
while( iterator )
{
std::cout << iterator->data << std::endl;
iterator = iterator->pNext;
}
// don't forget to free the allocated memory
Node *it = linkedList;
Node *temp;
while( it )
{
temp = it;
it = it->pNext;
delete temp;
}
}