//Filename: lab13e1.cpp //Author: Guillaume Nerzic //Created: 4/20/97 //Description: /* This program demonstrates more complex linked list structure. This program builds a list of student and attaches a linked list of course and grades to each on. It then prints out all the information and the de-allocates the list. */
#include <iostream.h> #include <fstream.h>
// Input file/ #define STUDENT_FILE "a:student.in"
//Student structure definition.
struct Student
{
int id;
char major[20];
};
//Course structure definition
struct Course
{
int course_no;
float final_grade;
Course* next;
};
// Node structure definition. Each node contains a student structure and a
// pointer to a linked list of courses.
struct Node
{
Student student;
Course* courseList;
Node* next;
};
// Function prototypes.
//Input and list building functions Node* readInput(); Node* readStudent(ifstream&); Node* addToListInOrder(Node*, Node*); Node* findPosition(Node*, Node*); Node* insertAfter(Node*, Node*, Node*); Course* readCourse(ifstream&); void addToStudent(Node*, Course*);
//Output functions void ListStudentByID(Node*); void printStudent(Node*); void printCourses(Course*);
//List destroying functions. Node* DeleteList(Node*); void DeleteCourses(Course*);
int main()
{
Node* studentList = NULL; //linked list pointer.
studentList = readInput(); //read list from input file.
ListStudentByID(studentList); //output list to screen
studentList = DeleteList(studentList);//destroy list.
return 0; }
/**
* Function: readList
* Description: Reads the data from a file and calls the current function to
* add a new student or a new course for a student.
* Parameters: N/A
* Returns: a pointer to the start of the linked list
*/
Node* readInput()
{
char code; //code in file is either 's' or 'c'
//'s' is information about a student.
//'c' is information about a course.
ifstream fileIn; //input file stream
Node* currentStudent = NULL; //latest student to be read from file.
Node* first = NULL; //first student in our ordered linked list
Course* currentCourse = NULL; //latest course to be read
fileIn.open(STUDENT_FILE);
fileIn >> code; //read code (priming read)
while(!fileIn.eof()) //while there is still data in file.
{
if (code=='s') //Adding another student
{
currentStudent = readStudent(fileIn);
first = addToListInOrder(first, currentStudent);
// Given the following input
s
123
comp sci
// you would get the following structures after readStudent and addToListInOrder (studied last week)

}
else //Adding another course to the current student.
{
currentCourse = readCourse(fileIn);
addToStudent(currentStudent, currentCourse);
}
fileIn >> code; //read next code.
}
fileIn.close();
return first; }
// Given the following input next
c
150
89.5
// you would get
Node* readInput()
{
char code; //code in file is either 's' or 'c'
//'s' is information about a student.
//'c' is information about a course.
ifstream fileIn; //input file stream
Node* currentStudent = NULL; //latest student to be read from file.
Node* first = NULL; //first student in our ordered linked list
Course* currentCourse = NULL; //latest course to be read
fileIn.open(STUDENT_FILE);
fileIn >> code; //read code (priming read)
while(!fileIn.eof()) //while there is still data in file.
{
if (code=='s') //Adding another student
{
currentStudent = readStudent(fileIn);
first = addToListInOrder(first, currentStudent);
}
else //Adding another course to the current student.
{
currentCourse = readCourse(fileIn);

addToStudent(currentStudent, currentCourse);
// Now call addToStudent
/**
* Function: addToStudent
* Description: This function will insert the new course as the new
* start of the linked list.
* Params: currentStudent - student that has taken this course.
* (holds head of the course linked list)
* currentCourse - node Course structure we are adding to the list.
* Returns: N/A
*/
void addToStudent(Node *currentStudent, Course *currentCourse)
{
currentCourse->next = currentStudent->courseList; currentStudent->courseList = currentCourse;
return; }
// Returning back to readInput
Node* readInput()
{
char code; //code in file is either 's' or 'c'
//'s' is information about a student.
//'c' is information about a course.
ifstream fileIn; //input file stream
Node* currentStudent = NULL; //latest student to be read from file.
Node* first = NULL; //first student in our ordered linked list
Course* currentCourse = NULL; //latest course to be read
fileIn.open(STUDENT_FILE);
fileIn >> code; //read code (priming read)
while(!fileIn.eof()) //while there is still data in file.
{
if (code=='s') //Adding another student
{
currentStudent = readStudent(fileIn);
first = addToListInOrder(first, currentStudent);
}
else //Adding another course to the current student.
{
currentCourse = readCourse(fileIn);
addToStudent(currentStudent, currentCourse);
} fileIn >> code; //read next code. }
fileIn.close();
return first; }
// Now if you read the following input and go back to the top of the read loop
c
170
76.2
// you would get
Node* readInput()
{
char code; //code in file is either 's' or 'c'
//'s' is information about a student.
//'c' is information about a course.
ifstream fileIn; //input file stream
Node* currentStudent = NULL; //latest student to be read from file.
Node* first = NULL; //first student in our ordered linked list
Course* currentCourse = NULL; //latest course to be read
fileIn.open(STUDENT_FILE);
fileIn >> code; //read code (priming read)
while(!fileIn.eof()) //while there is still data in file.
{
if (code=='s') //Adding another student
{
currentStudent = readStudent(fileIn);
first = addToListInOrder(first, currentStudent);
}
else //Adding another course to the current student.
{
currentCourse = readCourse(fileIn);
addToStudent(currentStudent, currentCourse);
// Now call addToStudent
void addToStudent(Node *currentStudent, Course *currentCourse)
{
currentCourse->next = currentStudent->courseList;
currentStudent->courseList = currentCourse;
return; }
// Returning back to readInput
Node* readInput()
{
char code; //code in file is either 's' or 'c'
//'s' is information about a student.
//'c' is information about a course.
ifstream fileIn; //input file stream
Node* currentStudent = NULL; //latest student to be read from file.
Node* first = NULL; //first student in our ordered linked list
Course* currentCourse = NULL; //latest course to be read
fileIn.open(STUDENT_FILE);
fileIn >> code; //read code (priming read)
while(!fileIn.eof()) //while there is still data in file.
{
if (code=='s') //Adding another student
{
currentStudent = readStudent(fileIn);
first = addToListInOrder(first, currentStudent);
}
else //Adding another course to the current student.
{
currentCourse = readCourse(fileIn);
addToStudent(currentStudent, currentCourse);
} fileIn >> code; //read next code. }
fileIn.close();
return first; }
// Now let's input another student with the input
S
111
EE
Node* readInput()
{
char code; //code in file is either 's' or 'c'
//'s' is information about a student.
//'c' is information about a course.
ifstream fileIn; //input file stream
Node* currentStudent = NULL; //latest student to be read from file.
Node* first = NULL; //first student in our ordered linked list
Course* currentCourse = NULL; //latest course to be read
fileIn.open(STUDENT_FILE);
fileIn >> code; //read code (priming read)
while(!fileIn.eof()) //while there is still data in file.
{
if (code=='s') //Adding another student
{
currentStudent = readStudent(fileIn);
first = addToListInOrder(first, currentStudent);
} else //Adding another course to the current student. { currentCourse = readCourse(fileIn); addToStudent(currentStudent, currentCourse); } fileIn >> code; //read next code. }
fileIn.close();
return first; }
// Given the following input next
c
150
85.7
// you would get
Node* readInput()
{
char code; //code in file is either 's' or 'c'
//'s' is information about a student.
//'c' is information about a course.
ifstream fileIn; //input file stream
Node* currentStudent = NULL; //latest student to be read from file.
Node* first = NULL; //first student in our ordered linked list
Course* currentCourse = NULL; //latest course to be read
fileIn.open(STUDENT_FILE);
fileIn >> code; //read code (priming read)
while(!fileIn.eof()) //while there is still data in file.
{
if (code=='s') //Adding another student
{
currentStudent = readStudent(fileIn);
first = addToListInOrder(first, currentStudent);
}
else //Adding another course to the current student.
{
currentCourse = readCourse(fileIn);

addToStudent(currentStudent, currentCourse);
// Now call addToStudent
/**
* Function: addToStudent
* Description: This function will insert the new course as the new
* start of the linked list.
* Params: currentStudent - student that has taken this course.
* (holds head of the course linked list)
* currentCourse - node Course structure we are adding to the list.
* Returns: N/A
*/
void addToStudent(Node *currentStudent, Course *currentCourse)
{
currentCourse->next = currentStudent->courseList; currentStudent->courseList = currentCourse;
return; }
// Returning back to readInput
Node* readInput()
{
char code; //code in file is either 's' or 'c'
//'s' is information about a student.
//'c' is information about a course.
ifstream fileIn; //input file stream
Node* currentStudent = NULL; //latest student to be read from file.
Node* first = NULL; //first student in our ordered linked list
Course* currentCourse = NULL; //latest course to be read
fileIn.open(STUDENT_FILE);
fileIn >> code; //read code (priming read)
while(!fileIn.eof()) //while there is still data in file.
{
if (code=='s') //Adding another student
{
currentStudent = readStudent(fileIn);
first = addToListInOrder(first, currentStudent);
}
else //Adding another course to the current student.
{
currentCourse = readCourse(fileIn);
addToStudent(currentStudent, currentCourse);
} fileIn >> code; //read next code. }
fileIn.close();
return first; }