doubly linked list problem
Feb 12, 2012 at 7:53pm UTC
My problem is creating the linked list. I'm trying to read the data for students from a file and store it in the list. I don't know how to store multiple pieces of data in a node. This is what I have.
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
#include <iostream>
#include <fstream>
using namespace std;
struct studentType
{
string studentID;
string firstname;
string lastname;
double salary;
studentType *next;
studentType *back;
};
void createList(studentType* first, studentType* last);
void printList(studentType* first);
void printListBack(studentType* last);
void insertFront(studentType* first);
void insertBack(studentType* last);
ifstream infile;
int main()
{
studentType *first, *last;
string ID, fname, lname;
int number;
createList(first, last);
system("PAUSE" );
return 0;
}
void createList(studentType*& first, studentType*& last)
{
studentType *newNode;
string ID, fname, lname;
int number;
first = NULL;
last = NULL;
infile.open("students.txt" );
while (infile)
{
for (int i=0; i<4; i++)
{
newNode = new studentType;
infile>>studentID>>firstname>>lastname>>salary;
newNode->studentID=ID;
newNode->firstname=fname;
newNode->lastname=lname;
newNode->salary=number;
newNode->next = NULL;
newNode->back = NULL;
}
if (first == NULL)
{
first = newNode;
last = newNode;
}
else
{
last->link = newNode;
last = newNode;
}
} // end of while-loop
}
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 UTC
Feb 13, 2012 at 8:27pm UTC
A line in the data file looks a little more like this
2214 Derek Anderson 50000
I made some changes and it runs but I can't figure out why it only outputs the last line of the file?
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
#include <iostream>
#include <fstream>
using namespace std;
struct studentType
{
string studentID;
string firstname;
string lastname;
double salary;
studentType *next;
studentType *back;
};
void createList(studentType*& first, studentType*& last);
void printList(studentType* first);
void printListBack(studentType* last);
void insertFront(studentType* first);
void insertBack(studentType* last);
ifstream infile;
int main()
{
studentType *first, *last;
createList(first, last);
printList(first);
system("PAUSE" );
return 0;
}
void createList(studentType*& first, studentType*& last)
{
studentType *newNode, *current;
first = NULL;
last = NULL;
current=first;
infile.open("students.txt" );
while (infile)
{
for (int i=0; i<4; i++)
{
newNode = new studentType;
infile >> newNode->studentID;
infile >> newNode->firstname;
infile >> newNode->lastname;
infile >> newNode->salary;
newNode->next = NULL;
newNode->back= current;
current=newNode;
}
if (first == NULL)
{
first = newNode;
last = newNode;
}
else
{
last->next = newNode;
last = newNode;
}
} // end of while-loop
}
void printList(studentType* first)
{
studentType *current;
current = new studentType;
current = first;
while (current !=NULL)
{
cout << current->studentID << "\t" ;
cout << current->firstname << "\t" ;
cout << current->lastname << "\t" ;
cout << current->salary << endl;
current=current->next;
}
}
Feb 13, 2012 at 9:23pm UTC
Problem solved! Thanks for the help.
Topic archived. No new replies allowed.