#include #include #include #include #define MAX_LINE 64 /*** ** Structure definition ***/ typedef struct _student { char* name; int id; struct _student* next; } Student; typedef struct _dorm { char* name; int dormNum; int numStudents; Student *studentList; struct _dorm* next; } Dorm; /*** ** One global variable ***/ Dorm *dormListHead = NULL; /*** ** Function prototypes: We have three set of functions ** 1. Part I : Functions you have to write ** 2. Part II : Functions that you can use for debugging ** 3. Part III: Functions that you dont have to touch ***/ /* Functions that you need to write */ void addDorm(char* name, int dormNum); void addStudent(char* name, int id); void graduate(int id); void move(char* name, int id, int dormNum); /* Function prototypes that you can use for debugging */ void printStudentList(Student* listHead); void printDorms(); /* Other function prototypes that you dont have to touch */ void usage(); int readDormFile(char* dormFileName); int readStudentFile(char* studentFileName); void releaseMemory(); /* Main function */ int main(int argc, char* argv[]){ if(argc != 3) { usage(); return -1; } readDormFile(argv[1]); readStudentFile(argv[2]); printDorms(); /* Release all the memory allocated */ releaseMemory(); return 0; } /** ** COMPLETE THE BELOW FUNCTIONS **/ void addDorm(char* name, int dormNum){ /** TODO: ** 1. Add a Dorm with 'name' (and 'dormNum') to the 'head' of the linked list pointed ** by 'dormListHead' ** 2. Set the 'numStudents' to zero **/ } void addStudent(char* name, int id){ /** TODO: ** 1. Add the student with 'name' (and 'id') to the least populated dorm ** 2. Add the student at the tail of the studentList of the Dorm. **/ } void graduate(int id){ /** TODO: ** 1. Find the dorm to which the student with 'name' and 'id' belongs ** 2. Remove that student from the studentList of the dorm **/ } void move(char* name, int id, int dormNum){ /** TODO: ** Move a student with 'name' and 'id' to the new dorm **/ } /** ** UTILITY FUNCTIONS ** Do not modify below this line **/ void usage(){ fprintf(stderr, "Usage: dorm \n"); } int readDormFile(char* dormFileName){ char line[MAX_LINE]; char name[MAX_LINE]; char* str; int id; FILE* f; if(dormFileName == NULL) { printf("Dorm file name is invalid\n"); return -1; } f = fopen(dormFileName, "r"); if(f == NULL) { printf("Dorm file (%s) is not present\n", dormFileName); return -1; } while(( str = fgets(line, MAX_LINE, f)) != NULL) { int len = strlen(line); if(len > 1 && line[len-1] == '\n') { line[len-1] = '\0'; len = strlen(line); if(len > 0) { sscanf(line, "%d %s",&id, name); addDorm(name, id); } } } fclose(f); return 0; } int readStudentFile(char* studentFileName){ char line[MAX_LINE]; char name[MAX_LINE]; char* str; int id; FILE* f; if(studentFileName == NULL) { printf("Student file name is invalid\n"); return -1; } f = fopen(studentFileName, "r"); if(f == NULL) { printf("Student file (%s) is not present\n", studentFileName); return -1; } while(( str = fgets(line, MAX_LINE, f)) != NULL) { int len = strlen(line); if(len > 1 && line[len-1] == '\n') { line[len-1] = '\0'; len = strlen(line); if(len > 0) { sscanf(line, "%d %s",&id, name); addStudent(name, id); } } } fclose(f); return 0; } void printStudentList(Student* listHead) { Student* stu; int i=0; for(stu = listHead; stu != NULL; stu = stu->next) { printf("\tStudent [%d]: %s (%d)\n", i, stu->name, stu->id); i++; } } void printDorms() { Dorm* dorm; for(dorm = dormListHead; dorm != NULL; dorm = dorm->next) { printf("Dorm [%d]: %s (%d)\n", dorm->dormNum, dorm->name, dorm->numStudents); printStudentList(dorm->studentList); printf("---------------------------------------------------------------------\n"); } } void releaseStudentList(Student* studentList) { Student * stu = studentList; Student * tmp = stu; while(stu){ free(stu->name); tmp = stu->next; free(stu); stu=tmp; } } void releaseMemory() { Dorm * dorm = dormListHead; Dorm * tmp = dorm; while(dorm) { releaseStudentList(dorm->studentList); free(dorm->name); tmp = dorm->next; free(dorm); dorm=tmp; } }