Load Data from a file to a Double Linked List in Memory
This function reads the data file in which the data items have been saved.Reads each data item,creates a node in memory and adds that node to the double linked list.As many nodes are created as the number of data items in the data file.
void loadlist()
{
if(!(first==NULL)) //list not empty in memory
{
deleteall();//delete existing list in memory
}
fstream listfile;
listfile.open("list.dls",ios::in) ;
listfile.seekg(0);//set file ptr at the beg of file
while(!listfile.eof())
{
NODE *temp=create();//create a blank node
if(temp) //memory allocation successful
{
listfile.read((char*)temp,sizeof(temp->data));// read the start data to temp
// now,if the file was empty it still has null at the end
// so 0 is read
if (temp->data==NULL)// if file is empty or we are at end;
{
cout<<"\n\n\t\tFile is empty OR End of File reached ...";
cout<<"\n\n\t\t Press a key to continue ... !";getch();
delete temp;// no need to load a list now
}
else
{
//valid data read
cout<<"\n\n\t\tdata read from file :"<<temp->data;
addattail(temp); //addattail caters to the node being added to an empty list
}
}
else
{
cout<<"\nUnable to load list,Out of memory ! Press a key to continue ...";getch();
}
}
listfile.close();
}
Continue reading the remaining 1 pages of the article through page links below ....
