Question
#include #include #include #include // needed to use tolower() #pragma warning(disable: 4996) // for Visual Studio Only #define MAX_NAME_LENGTH 35 // global linked list 'list'
#include
#define MAX_NAME_LENGTH 35
// global linked list 'list' contains the list of students struct studentList { struct student *student; struct studentList *next; } *list = NULL; // currently empty
// structure "student" contains the student's name, library's name and linked list of courses struct student { char name[MAX_NAME_LENGTH]; unsigned int rollNumber; struct course *courses; };
// linked list of 'course' contains name of courses struct course { char name[MAX_NAME_LENGTH]; struct course *next; };
// forward declaration of functions (already implmented) void flushStdIn(); void executeAction(char);
// functions that need implementation: // HW 7 void addStudent(char* studentNameInput, int rollNoInput); // 15 points void displayStudentList(struct studentList* tempList); // 10 points struct student* searchStudent(char* studentNameInput); // 10 points void removeStudent(char* studentNameInput); // 15 points //HW 8 void addCourse(char* studentNameInput, char* courseInput); // 15 points char* lastCourse(char* studentNameInput); // 15 points void displayStudentAndCourseList(struct studentList* tempList); // 10 points // edit removeStudent() // 10 points
int main() { char choice = 'i'; // initialized to a dummy value do { printf(" CSE240 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 // Read the function case by case void executeAction(char c) { char studentNameInput[MAX_NAME_LENGTH], courseInput[MAX_NAME_LENGTH]; unsigned int rollNoInput; struct student* searchResult = NULL;
switch (c) { case 'a': // add student // input student details from user printf(" Please enter student name: "); fgets(studentNameInput, sizeof(studentNameInput), stdin); studentNameInput[strlen(studentNameInput) - 1] = '\0'; // discard the trailing ' ' char printf("Please enter roll number: "); scanf("%d", &rollNoInput); flushStdIn();
// if (searchStudent(studentNameInput) == NULL) // un-comment this line after implementing searchStudent() if (1) // comment out this line after implementing searchStudent() { addStudent(studentNameInput, rollNoInput); printf(" Student successfully added to the list! "); } else printf(" That student is already on the list! "); break;
case 'd': // display the list displayStudentList(list); break;
case 'r': // remove student printf(" Please enter student name: "); fgets(studentNameInput, sizeof(studentNameInput), stdin);
studentNameInput[strlen(studentNameInput) - 1] = '\0'; // discard the trailing ' ' char //if (searchStudent(studentNameInput) == NULL) // un-comment this line after implementing searchStudent() if (0) // comment out this line after implementing searchStudent() 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'; // discard the trailing ' ' char
// if (searchStudent(studentNameInput) == NULL) // un-comment this line after implementing searchStudent() if (0) // comment out this line after implementing searchStudent() 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'; // discard the trailing ' ' char 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'; // discard the trailing ' ' char
// if (searchStudent(studentNameInput) == NULL) // un-comment this line after implementing searchStudent() if (0) // comment out this line after implementing searchStudent() printf(" Student name does not exist or the list is empty! "); else { printf(" Last course added: %s ", lastCourse(studentNameInput)); } break;
case 'b': // display student details and courses displayStudentAndCourseList(list); break;
case 'q': // quit break;
default: printf("%c is invalid input! ", c); } }
// HW7 Q1: addStudent (15 points) // This function is used to insert a new student into the list. // You must insert the new student to the head of linked list 'list'. // You need NOT check if the student already exists in the list because that is taken care by searchStudent() called in executeAction(). Look at how this function is used in executeAction(). // Don't bother to check how to implement searchStudent() while implementing this function. Simple assume that student does not exist in the list while implementing this function. // NOTE: The function needs to add the student to the head of the list, unlike the previous HW. // NOTE: This function does not add courses to the student info. There is another function addCourse() in HW8 for that. // Hint: In this question, no courses means NULL courses.
void addStudent(char* studentNameInput, int rollNoInput) {
}
// HW7 Q2: displayStudentList (10 points) // This function displays the student details (struct elements) of each student. // Parse through the linked list 'list' and print the student details (student name and roll no.) one after the other. See expected output screenshots in homework question file. // You should not display course names (because they are not added in HW7). // You MUST use recursion in the function to get full points. Notice that 'list' is passed to the function argument. Use recursion to keep calling this function till end of list.
void displayStudentList(struct studentList* tempList) {
}
// HW7 Q3: searchStudent (10 points) // This function searches the 'list' to check if the given student exists in the list. Search by student name. // If it exists then return the 'student' node of the list. Notice the return type of this function. // If the student does not exist in the list, then return NULL. // NOTE: After implementing this fucntion, go to executeAction() to comment and un-comment the lines mentioned there which use searchStudent() // in case 'a', case 'r', case 'b', case 'l' (total 4 places) struct student* searchStudent(char* studentNameInput) {
return NULL; // comment out this line when implementing the function // it is placed here to avoid compile error in empty function }
// HW7 Q4: removeStudent (15 points) // This function removes a student from the list. // Parse the list to locate the student and delete the 'student' node. // You need not check if the student exists because that is done in executeAction() // NOTE: In HW 8, you will need to add code to this function to remove courses as well, when you remove the student.
void removeStudent(char* studentNameInput) { struct studentList* tempList = list; // work on a copy of 'list' }
You are given a partially completed program hw7_ac. Your should follow the instructions given in the program to complete the functions so that the program executes properly. You will be completing a program that creates a list of students. In HW7, the lowest linked list 'course' does not come into picture. It is a menu driven program where user is given following options: a) Add a student's information (name and roll number).In HW7, the new student needs to be added to the head of the linked list. We do not ask user for courses to add in this function. So simply assign NULL to "courses member of the student' node when a new student is added to the list in this function. (courses is used in Hw8) b) Display the student list. This function should print student's name and roll number. It should not print courses of the student. You must write a recursive function to implement this function. You may print the list forward or backward. See expected output for this function below. c) Search a student in the list by name. This function should return the 'student' node if that student is found in the list, else return NULL. It is used as helper function in executeAction() to check if the student exists in the list. d) Remove the student from the list. This function removes the student's name and roll numberStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
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