#include #include #include #include #define MAX_NAME_LENGTH 35 struct studentList { struct student *student; struct studentList *next; } *list = NULL; struct student { char name[MAX_NAME_LENGTH];
#include
#define MAX_NAME_LENGTH 35
struct studentList { struct student *student; struct studentList *next; } *list = NULL;
struct student { char name[MAX_NAME_LENGTH]; unsigned int rollNumber; struct course *courses; };
struct course { char name[MAX_NAME_LENGTH]; struct course *next; };
void flushStdIn(); void executeAction(char);
void addStudent(char* studentNameInput, int rollNoInput); void displayStudentList(struct studentList* tempList); struct student* searchStudent(char* studentNameInput); void removeStudent(char* studentNameInput); void addCourse(char* studentNameInput, char* courseInput); char* lastCourse(char* studentNameInput); void displayStudentAndCourseList(struct studentList* tempList); void displayCourses(struct course* course_head); // edit removeStudent()
int main() { char choice = 'i'; do { printf(" HW 7,8 "); printf("Please enter your selection: "); printf("HW7: "); printf("\t a: add a new student to the list "); printf("\t d: display student list (no courses) "); printf("\t r: remove a student "); printf("\t q: quit "); printf("HW8: "); printf("\t c: add course of a student "); printf("\t l: display last added course of a student "); printf("\t b: display student list including courses "); printf("\t q: quit "); choice = tolower(getchar()); flushStdIn(); executeAction(choice); } while (choice != 'q');
return 0; }
// flush out leftover ' ' characters void flushStdIn() { char c; do c = getchar(); while (c != ' ' && c != EOF); }
// Ask for details from user for the given selection and perform that action void executeAction(char c) { char studentNameInput[MAX_NAME_LENGTH], courseInput[MAX_NAME_LENGTH]; unsigned int rollNoInput; struct student* searchResult = NULL;
switch (c) { case 'a': printf(" Please enter student name: "); fgets(studentNameInput, sizeof(studentNameInput), stdin); studentNameInput[strlen(studentNameInput) - 1] = '\0'; printf("Please enter roll number: "); scanf("%d",& rollNoInput); flushStdIn();
if (searchStudent(studentNameInput) == NULL) { addStudent(studentNameInput, rollNoInput); printf(" Student successfully added to the list! "); } else printf(" That student is already on the list! "); break;
case 'd': displayStudentList(list); break;
case 'r': printf(" Please enter student name: "); fgets(studentNameInput, sizeof(studentNameInput), stdin);
studentNameInput[strlen(studentNameInput) - 1] = '\0'; if (searchStudent(studentNameInput) == NULL) printf(" Student name does not exist or the list is empty! "); else { removeStudent(studentNameInput); printf(" Student successfully removed from the list! "); } break;
case 'c': // add course printf(" Please enter student name: "); fgets(studentNameInput, sizeof(studentNameInput), stdin); studentNameInput[strlen(studentNameInput) - 1] = '\0';
if (searchStudent(studentNameInput) == NULL) printf(" Student name does not exist or the list is empty! "); else { printf(" Please enter course name: "); fgets(courseInput, sizeof(courseInput), stdin); courseInput[strlen(courseInput) - 1] = '\0'; addCourse(studentNameInput, courseInput); printf(" Course added! "); } break;
case 'l': // last course printf(" Please enter student name: "); fgets(studentNameInput, sizeof(studentNameInput), stdin); studentNameInput[strlen(studentNameInput) - 1] = '\0';
if (searchStudent(studentNameInput) == NULL) printf(" Student name does not exist or the list is empty! "); else { printf(" Last course added: %s ", lastCourse(studentNameInput)); } break;
case 'b': displayStudentAndCourseList(list); break;
case 'q': break;
default: printf("%c is invalid input! ", c); } }
// addStudents
void addStudent(char* studentNameInput, int rollNoInput) { //make a new student node that will be pointed to by the head of the list struct student* new_student = (struct student*) malloc(sizeof(struct student));
//populate the new student node with parameters strcpy(new_student->name, studentNameInput); new_student->rollNumber = rollNoInput; new_student->courses = NULL; //redundant? //add a new node to studentList. This node will be the new head of the list struct studentList* new_studentList = (struct studentList*) malloc(sizeof(struct studentList));
//populate the new studentList node with parameter new_studentList->student = new_student;
/ew studentList node next gets pointed to head of current studentList likedList new_studentList->next = list;
//move up head so that it points to the current head of the list list = new_studentList;
}
// displayStudentList
void displayStudentList(struct studentList* tempList) { if (tempList == NULL){ return; } else { printf(" Student name: %s", tempList->student->name); printf(" Student roll Number: %d ", tempList->student->rollNumber); displayStudentList(tempList->next); } }
// searchStudent struct student* searchStudent(char* studentNameInput) { struct studentList* tempList = list;
if (tempList == NULL) { return NULL; } else {
while (tempList != NULL) { if (strcmp(tempList->student->name, studentNameInput) == 0) { return tempList->student; } tempList = tempList->next; } return NULL; }
//return NULL; }
// modify removeStudent
void removeCourses(struct student* current_student) {
struct course* temp = current_student->courses;
while (current_student->courses != NULL) { current_student->courses = current_student->courses->next; free(temp); temp = current_student->courses; }
}
//removeStudent
void removeStudent(char* studentNameInput) { struct studentList* tempList = list; struct studentList* follower = list;
// remove head node if (strcmp(tempList->student->name, studentNameInput) == 0) {
removeCourses(tempList->student);
tempList = tempList->next; //free(list); list = tempList; return; }
//cycle until you find the input node while (strcmp(tempList->student->name, studentNameInput) != 0) { follower = tempList; tempList = tempList->next; } removeCourses(tempList->student); follower->next = tempList->next; free(tempList); }
// addCourse
void addCourse(char* studentNameInput, char* courseInput) { struct studentList* tempList = list; //printf(" tempList: %p ", tempList); //printf("list: %p ", list); //indexes to proper node in studentList while (tempList->next != NULL){ if (strcmp(tempList->student->name, studentNameInput) == 0) { break; } else { tempList = tempList->next; //printf(" inside while - "); //printf(" tempList: %p ", tempList); //printf("list: %p ", list); } } //printf(" after while - "); //printf(" tempList: %p ", tempList); //printf("list: %p ", list);
//allocate memory for new course to added to list struct course* new_course = (struct course*) malloc(sizeof(struct course*)); //populate new course //printf(" ---0 ");
strcpy(new_course->name, courseInput);
//printf(" ---1 ");
//place in list if (tempList->student->courses == NULL) { //printf(" ---2 "); tempList->student->courses = new_course; new_course->next = NULL; //printf(" ---3 "); } else { //case: course list is not null, insert to head of node new_course->next = tempList->student->courses; tempList->student->courses = new_course; } }
// lastCourse
char* lastCourse(char* studentNameInput) { //struct studentList* tempList = list;
//returns right student return (searchStudent(studentNameInput))->courses->name;
//return NULL; }
// displayStudentAndCourseList
void displayStudentAndCourseList(struct studentList* list) { struct studentList* temp = list; if (temp == NULL) { return; } else { printf(" Student name: %s", temp->student->name); printf(" Student roll Number: %d ", temp->student->rollNumber); printf("Courses taken: "); displayCourses(temp->student->courses); displayStudentAndCourseList(temp->next); } }
//helper function for displayStudentAndCourseList recursively prints the course list given a course list head void displayCourses(struct course* course_head) {
if (course_head == NULL) { //base case return; } else { printf("\t%s ",course_head->name); displayCourses(course_head->next); } }
// HW8 Q1: addCourse (15 points) // This function adds course's name to student info. // Parse the list to locate the student and add the course to the 'courses' linked list. No need to check if the student name exists on the list. That is done in executeAction(). // If the 'courses' list is empty, then add the course. If the student has existing courses, then you may add the new course to the head or the tail of the 'courses' list. // You can assume that the same course name does not exist. So no need to check for existing course names, like we do when we add new student. // NOTE: Make note of whether you add the course to the head of 'courses' list or the tail. You will need that info when you implement lastCourse() // (Sample solution has courses added to the tail of 'courses'. You are free to add new course to head or tail of 'courses' list.)
void addCourse(char* studentNameInput, char* courseInput) { struct studentList* tempList = list; // work on a copy of 'list'
}
// HW8 Q2: lastCourse (15 points) // This function returns the name of the last (most recently) added course of a student. // Parse the list to locate the student. No need to check if the student name exists in the list. That is done in executeAction(). // Then parse the course names to return the most recently added course. // NOTE: Last course does not necessarily mean the last course in the 'courses' list. It means the most recently added course. // If you are adding courses to the head of the list in addCourse(), then you should return that course accordingly.
char* lastCourse(char* studentNameInput) { struct studentList* tempList = list; // work on a copy of 'list'
return NULL; // comment out this line when implementing the function // it is placed here to avoid compile error in empty function }
// HW8 Q3: displayStudentAndCourseList (10 points) // This function displays every detail of each student, including courses. // Parse through the linked list and print the student details (student name, roll no., courses) one after the other. See expected output screenshots in homework question file. // Notice that 'list' is passed to the function as argument. // NOTE: You may re-use HW7 Q2 displayStudentList(list) code here.
void displayStudentAndCourseList(struct studentList* tempList) { }
// HW8 Q4: modify removeStudent (10 points) // In HW7, removeStudent() is supposed to remove student details like student name and roll number. // Modify that function to remove courses of the student too. // When the student is located in the 'list', after removing the student name and number of copies, parse the 'courses' list of that student // and remove the courses.
HW8 homework is the continuation of HW 7. It focuses on using 'course' linked list. In this part, the user should be able to use following menu options: a) Add a course to a student's profile. This function assumes that the student is added in the list and assigns course name using the *courses member of 'student' node. You may add the new course to the head or tail of the 'course' linked list. (Sample solution adds the course to the tail) b) Display the last added course for a student. 'Add course' option can be used multiple times to add multiple courses under a student's profile. This function should return the most recently added course of the student. See expected output below. c) Display the list of students and their course list. This function should display student's name, roll number and courses. See expected output below. (Display function in HW 7 did not display courses of the student) HW8 homework is the continuation of HW 7. It focuses on using 'course' linked list. In this part, the user should be able to use following menu options: a) Add a course to a student's profile. This function assumes that the student is added in the list and assigns course name using the *courses member of 'student' node. You may add the new course to the head or tail of the 'course' linked list. (Sample solution adds the course to the tail) b) Display the last added course for a student. 'Add course' option can be used multiple times to add multiple courses under a student's profile. This function should return the most recently added course of the student. See expected output below. c) Display the list of students and their course list. This function should display student's name, roll number and courses. See expected output below. (Display function in HW 7 did not display courses of the student)Step by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started