Feb 13, 2012 at 5:04am Feb 13, 2012 at 5:04am UTC
I am going to assume that a line in your data file looks like:
1 2
001 Mickeal Hodgekins 10.00
002 Jeana Goggie 11.11
Please let me know if I am correct.
If this is the case I would expect:
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
#include <iostream>
#include <fstream>
using namespace std;
struct studentType
{
string studentID;
string firstname;
string lastname;
double salary;
studentType *next;
studentType *back;
};
studentType* CreatList();
{
studentType *listHead = NULL;
studentType *currentNode = NULL;
studentType *newNode = NULL;
infile.open("students.txt" );
while (infile)
{
// this assumes the list is sequential in the file.
newNode = new studentType;
infile >> newNode->studentID;
infile >> newNode->firstname;
infile >> newNode->lastname;
infile >> newNode->salaray;
NewNode->next = null;
newNode->back = currentNode;
// keep track of this node for the next pass of data for the back pointer.
currentNode = newNode;
if (listHead == NULL)
{
// build the empty list
ListNead = newNode;
}
else
{
// find the tail of the list and append;
studenttype *pSearch = listHead;
while (pSearch->next)
{
pSearch = pSearch->next;
}
pSearch->next= newNode;
}
} // while on the file.
return listHead;
}
int main()
{
studentType *pListhead = CreateList();
studentType *pCurrentNode = pListHead;
if (pListHead)
{
while (pCurrentNode->next)
{
cout << pCurrentNode->studentID << "\t" ;
cout << pCurrentNode->firstname << "\t" ;
cout << pCurrentNode->lastname << "\t" ;
cout << pCurrentNode->salary << endl;
pCurrentNode = pCurrentNode->next;
}
//pCurrentNode should be at the bottom of the list when I hit here.
cout << endl << "Backwards" << endl;
while (pCurrentNode->back)
{
cout << pCurrentNode->studentID << "\t" ;
cout << pCurrentNode->firstname << "\t" ;
cout << pCurrentNode->lastname << "\t" ;
cout << pCurrentNode->salary << endl;
pCurrentNode = pCurrentNode->back;
}
}
return 0;
}
Last edited on Feb 13, 2012 at 5:08am Feb 13, 2012 at 5:08am UTC
Feb 13, 2012 at 9:23pm Feb 13, 2012 at 9:23pm UTC
Problem solved! Thanks for the help.