//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)

p1-1.gif (2804 bytes)

 

	}
      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);

p1-2.gif (3724 bytes)

	  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)
{
p1-3.gif (4491 bytes)
  currentCourse->next = currentStudent->courseList;
  currentStudent->courseList = currentCourse;
p1-4.gif (4744 bytes)
  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);
p1-5.gif (4079 bytes)
	}
      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);
p1-6.gif (4319 bytes)
	  addToStudent(currentStudent, currentCourse);

 

// Now call addToStudent

void addToStudent(Node *currentStudent, Course *currentCourse)
{
p1-7.gif (4962 bytes)
  currentCourse->next = currentStudent->courseList;
p1-8.gif (5194 bytes)
  currentStudent->courseList = currentCourse;
p1-9.gif (5298 bytes)
  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);
p1-10.gif (4718 bytes)
	}
      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);
p1-11.gif (5224 bytes)
	}
      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);

p1-12.gif (5416 bytes)

	  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)
{
p1-13.gif (6704 bytes)
  currentCourse->next = currentStudent->courseList;
  currentStudent->courseList = currentCourse;
p1-14.gif (8877 bytes)
  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);
p1-15.gif (5567 bytes)
	}
      fileIn >> code;     //read next code.
    }
  fileIn.close();
  return first;
}